Search code examples
phparrayspreg-split

How can I split this array in PHP?


Noob question. I have this:

Array
(
    [0] => address = 123 Something Street
    [1] => address2 = Something else
    [2] => city = Kalamazoo
    [3] => state = MI
    [4] => zip = 49097
    [5] => country = United States
)

but I want this:

Array
(
    [address] => 123 Something Street
    [address2] => Something else
    [city] => Kalamazoo
    [state] => MI
    [zip] => 49097
    [country] => United States
)

How do I do this? Thanks!


Solution

  • Try this:

    $new_array = array();
    foreach($your_array as $line) {
     list($key, $value) = explode('=', $line, 2);
     $new_array[$key] = $value;
    }
    

    Access $new_array.