0

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 ... ?

3
  • What do you mean your "Variables is Dynamic" ? You can change values of an array you know? Commented Sep 30, 2013 at 20:28
  • 3
    How do you get that variable in the first place? Commented Sep 30, 2013 at 20:29
  • I'm updated my question Commented Sep 30, 2013 at 20:56

6 Answers 6

3
$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

Sign up to request clarification or add additional context in comments.

1 Comment

Better yet, prevent whatever is creating that array-ish string and just use the array. If you really need to work a string then serialize the variable - don't come up with your own serialization strategy.
1

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!

4 Comments

i get this code with your help : Array ( [0] => Array ( [item_id] => [parent_id] => none [depth] => 0 [left] => 1 [right] => 18 ) [1] => Array ( [item_id] => 1 [parent_id] => [depth] => 1 [left] => 2 [right] => 7 ) [2] => Array ( [item_id] => 7 [parent_id] => 1 [depth] => 2 [left] => 3 [right] => 4 ) )
and use this foreach ($json as $key=>$value) {} | now how i can get item_id ?
@EbadGhafoory: Does the for loop not satisfy your requirements? You can simply echo $item_id[1]; outside the loop, and it'd print the value.
@EbadGhafoory: I've updated the answer with the foreach version, too :)
1

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

Comments

1

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.

4 Comments

This will work but in general eval() should be avoided.
@Mike B, I agree that eval() should be avoided if one handles user input string. There is no risk if the code is from the developer's side.
Keep reading. There are plenty of reasons outside of code maintainability and security to avoid eval. eval()`ed code can not be opcode cached.
@Mike B, The opcode cache is a good argument. Nevertheless, eval() is the simplest way to accomplish this task...
0

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 )

Comments

0

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 );

2 Comments

You'll find issues with the "null" string (rather then a null empty value). A better solution would be to fix the generator of that array.
Well, of course i would consider figuring out why i am facing with such non-standard variable in the first place. Although what you're suggesting is not a flaw in my example. The input is "null" so as the output. Try without the quotes on null and you'll get the empty string instead. json_decode handles it just fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.