0

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.

i print it out in the console.log() and i get this array:

array(1) {
  [16]=>
  array(2) {
    [3488]=>
    array(1) {
      [0]=>
      array(2) {
        ["article_no_internal"]=>
        string(6) "999184"
        ["article_name_internal"]=>
        string(29) "Geschenkbox Kerzenschein 2011"
      }
    }
    [2615]=>
    array(1) {
      [0]=>
      array(2) {
        ["article_no_internal"]=>
        string(6) "700469"
        ["article_name_internal"]=>
        string(29) "Hotelscheck RomantischeTagef2"
      }
    }
  }
}

This is about right. How can i access the article_name of the second array, with the ID 2615?

found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.

EDIT:

As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.

3
  • You get that output from console.log?! That looks like var_dump in PHP. How exactly are you passing that array? Commented Aug 29, 2012 at 10:55
  • @deceze you are right it was a lost var_dump in php. Now it seems i dont have anything in my console this is how i encode: $shipping_part_list_array = json_encode($db_obj->getArticleList($elements),true); and how i pass to javascript: var array = <?php echo $shipping_part_list_array; ?>; in the console now i have undefined. Commented Aug 29, 2012 at 11:02
  • if i go like this var array = <?php echo json_encode($shipping_part_list_array); ?>; it says there are no childe objects. Commented Aug 29, 2012 at 11:43

1 Answer 1

0

Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.

For example you can access:

jsonObject[2615][0]["article_name_internal"]

if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.

var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]
Sign up to request clarification or add additional context in comments.

1 Comment

json_encode($db_obj->getArticleList($elements),true); this is how i encode in php var array = <?php echo $shipping_part_list_array; ?>; and like this i pass the array, but when i console.log(array); i just get undefined prototype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.