0

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Marek Schubert
  • 107
  • 1
  • 2
  • 10

4 Answers4

4

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;
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • or use a JSON... also, how'll your code know ahead which variables were created and how to use them – kishu27 Jan 10 '13 at 20:08
  • @kishu27: How does the OP know that? This is what the OP wanted. How he wants to use them is up to him. – gen_Eric Jan 10 '13 at 20:11
  • @kishu27 A JSON would be one big string, that doesn't seem very useful. – Barmar Jan 10 '13 at 20:12
  • @kishu27 if OP wants to output data as it is shown in question just replace `$$name = $val;` with `echo $name . "=" . $val . "
    ";`
    – nanobash Jan 10 '13 at 20:13
  • @RocketHazmat understood but lets say string comes with a monkey=9 and he didn't code for using a $monkey, it doesn't solve the purpose. associative array makes much more sense and OP can loop through it to find both variable names and value. of course there can be a 100 examples of what OP wants is a good idea, but with the data provided I guess Assoc Arrays are a better design. just my thought :) – kishu27 Jan 10 '13 at 20:18
  • @Barmar I should have commented in more detail, i'm sorry. I meant that he could use JSON strings in place of the string that he is showing and convert it to Assoc Arrays and use. My vote goes to Assoc arrays and I wrote a supporting statement for that. should've added more details to my comment :) – kishu27 Jan 10 '13 at 20:19
4

You can try to join the array and parse the string into variables

parse_str(implode('&',$item));
vvolkov
  • 1,172
  • 8
  • 10
0

You can do it like this

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


foreach($item as $row)
{
    $new    =   explode('=',$row);
    $array[$new[0]] =   $new[1];
}

extract($array);
echo $apple;
echo $banana;
echo $orange;
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
0

Your array looks like someone exploded an ini file on its newlines.

Implode with newlines, then parse, then call extract() to generate the variables. (Demo)

extract(
    parse_ini_string(
        implode(PHP_EOL, $item)
    )
);

echo "$apple $banana $orange";

Using explode() or parse_ini_string() or preg_ functions (etc.) will render the numeric values as string-type.

If you'd like the numeric values to be integer-typed, then using %d in the "format" parameter of sscanf() will be most direct. (Demo1)

foreach ($item as $string) {
    [$key, $$key] = sscanf($string, '%[^=]=%d');
}

Generally speaking, I do not advise the generation of individual variable (I more strenuously advise against variable variables). Perhaps an associative array would be suitable. (Demo)

$result = [];
foreach ($item as $string) {
    [$key, $result[$key]] = sscanf($string, '%[^=]=%d');
}
var_dump($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136