Q:

Write a php program to convert the given string into an array

belongs to collection: PHP Programming Exercises

0

PHP convert string into an array

Write a php program to convert the given string into an array

Suppose the string is -

$a = 'Burch Jr, Philip H., The American establishment, Research in political economy 6(1983), 83-156';

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

Sometimes, arrays are not used to store information; instead, a string is used. The single values are all within the string, but are separated by a special character.

The following example convert string into array -

<?php
$csvdata = 'Burch Jr, Philip H., The American establishment, Research in political economy 6(1983), 83-156';
$a = explode(',', $csvdata);
$info = print_r($a, true);
echo "<pre>$info</pre>";
?>

The PHP function explode() creates an array out of these values, you just have to provide the character(s) at which the string needs to be split.

Output of the above code -

Array
(
    [0] => Burch Jr
    [1] =>  Philip H.
    [2] =>  The American establishment
    [3] =>  Research in political economy 6 (1983)
    [4] =>  83-156
)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a php program to loop over the json data... >>
<< Write a php program to store the username in cooki...