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

Community
  • 1
  • 1
Ebad ghafoory
  • 1,332
  • 3
  • 14
  • 25

6 Answers6

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

Steven
  • 13,250
  • 33
  • 95
  • 147
  • 1
    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. – Mike B Sep 30 '13 at 20:40
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

Vincent
  • 22,366
  • 18
  • 58
  • 61
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.

jacouh
  • 8,473
  • 5
  • 32
  • 43
  • This *will* work but in general `eval()` [should be avoided](http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php/951868). – Mike B Sep 30 '13 at 20:44
  • @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. – jacouh Sep 30 '13 at 20:48
  • 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 Sep 30 '13 at 20:50
  • @Mike B, The opcode cache is a good argument. Nevertheless, eval() is the simplest way to accomplish this task... – jacouh Sep 30 '13 at 20:57
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!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 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 ) ) – Ebad ghafoory Sep 30 '13 at 21:07
  • and use this foreach ($json as $key=>$value) {} | now how i can get item_id ? – Ebad ghafoory Sep 30 '13 at 21:08
  • 1
    @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. – Amal Murali Sep 30 '13 at 21:09
  • 1
    @EbadGhafoory: I've updated the answer with the `foreach` version, too :) – Amal Murali Sep 30 '13 at 21:16
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 );
Varol
  • 1,798
  • 1
  • 22
  • 30
  • 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. – Steven Sep 30 '13 at 20:44
  • 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. – Varol Sep 30 '13 at 20:50
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 )
nurakantech
  • 502
  • 4
  • 14