phparraysinitext-parsingdelimited

Convert array of delimited strings to variables


I need to convert all items in array to variables like:

$item[0] = "apple=5";
$item[1] = "banana=7";
$item[2] = "orange=8";

And I want to convert it to this:

$apple = 5;
$banana = 7;
$orange = 8;

Like normal variables. Is it possible?


Solution

  • Seems like a silly thing to do, why not convert it into an associative array? But if you must:

    foreach($item as $x) {
      list($name, $val) = explode('=', $x, 2);
      $$name = $val;
    }