Rawat_ji Rawat_ji - 1 year ago 79
PHP Question

How to adjust value of a "User define String" into a Array in PHP

I am a newbie in php & sorry if it's a basic question.
i have a variable which contains string values

Its User define means the number of values in string may vary (user could choose upto Z)

$raw_data= "A=400 B=500 C=300";

Now i want these values in a array which should be like:

$data = array (array('A', 400),array('B', 500),array('C', 300));

any help ???
Thanks in Advance.

Answer Source
$data = [];
foreach (explode(" ", $raw_data) as $part) $data[] = explode("=", $part);

This will give you the $data you want in your code above. Alternatively you can use array_map.

$data = array_map(function($d) {
    return explode("=", $d);
}, explode(" ", $raw_data));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download