Ebad ghafoory
Ebad ghafoory

Reputation: 1304

convert a variable to an array in php

I have this Variable :

$value = '"item_id"=>"null","parent_id"=>"none","depth"=>0,"left"=>"1","right"=>18';

And I want get item_id and other element from top Variable with Array method, so i write this :

$value_arr = array($value);
$item_id = $value_arr["item_id"];

but i get error Notice: Undefined index: item_id in file.php on line 115

but When i use this method i get fine result successfully :

$value_arr = array("item_id"=>"null","parent_id"=>"none","depth"=>0,"left"=>"1","right"=>18);
$item_id = $value_arr["item_id"];

How i can solve this problem ?

Note: i don't want use 2'nd method because my Variables is Dynamic

UPDATE:

Vincent answered that i must use json_decode and i want to ask another question for better way because my original string that i have is :

[
{"item_id":null,"parent_id":"none","depth":0,"left":"1","right":18},
{"item_id":"1","parent_id":null,"depth":1,"left":2,"right":7},
{"item_id":"3","parent_id":null,"depth":1,"left":2,"right":7}
]

With this information whats the better way for get item_id, parent_id and ... ?

Upvotes: 0

Views: 221

Answers (6)

Amal
Amal

Reputation: 76666

Use json_decode() with second parameter as TRUE to get an associative array as result:

$json = json_decode($str, TRUE);    
for ($i=0; $i < count($json); $i++) { 
    $item_id[$i] = $json[$i]['item_id'];
    $parent_id[$i] = $json[$i]['parent_id'];
    // ...
}

If you want to do it using a foreach loop:

foreach ($json as $key => $value) {
    echo $value['item_id']."\n";
    echo $value['parent_id']."\n";
    // ...
}

Demo!

Upvotes: 1

Varol
Varol

Reputation: 1858

A quick and dirty solution could be:

$array = json_decode( '{' . str_ireplace( '=>', ':', $value ) . '}', true );
// Array ( [item_id] => null [parent_id] => none [depth] => 0 [left] => 1 [right] => 18 )

EDIT: In regards to the update of the question.

Your input is a json_encoded array. simply json_decode it and you're done.

json_decode( $value, true );

Upvotes: 0

nurakantech
nurakantech

Reputation: 502

This can be a solution you are looking for:

<?php
     $value = '"item_id"=>"null","parent_id"=>"none","depth"=>0,"left"=>"1","right"=>18';
     $arr = explode(',',$value);
     foreach($arr as $val)
     {
      $tmp = explode("=>",$val);
      $array[$tmp[0]] = $tmp[1];
     }
   print_r($array);
?>

And this will output something like:

Array ( ["item_id"] => "null" ["parent_id"] => "none" ["depth"] => 0 ["left"] => "1" ["right"] => 18 )

Upvotes: 0

jacouh
jacouh

Reputation: 8769

I tested this for you:

<?php
$value = '"item_id"=>"null","parent_id"=>"none","depth"=>0,"left"=>"1","right"=>18';
eval("\$value_arr = array($value);");
print_r($value_arr);
?>

Please check. PHP::eval() is used. It worked.

Upvotes: 1

Steven
Steven

Reputation: 13995

$value = '"item_id"=>"null","parent_id"=>"none","depth"=>0,"left"=>"1","right"=>18';

Is not a PHP array, you will need to convert that to an array by exploding it on "=>" and "," and remove any extra "'s you find.

You should be using JSON however and using json_encode and json_decode

Upvotes: 3

Vincent
Vincent

Reputation: 22954

You should use JSON encoding and use the json_decode method if you want something dynamic. JSON is a good standard for dynamic data.

http://php.net/manual/en/function.json-decode.php

Upvotes: 1

Related Questions