0

I want to pass a array of array from javascript into php. what is the simplest way to do it?

Array in javascript likes:

var resultArray = [ 
                    {"id":"1", "description":"aaa", "name":"zzz", "titel":"mmm"},
                    {"id":"2", "description":"bbb", "name":"yyy", "titel":"nnn"},
                    {"id":"3", "description":"ccc", "name":"xxx", "titel":"lll"},
                    {"id":"4", "description":"ddd", "name":"www", "titel":"qqq"},
                    {"id":"5", "description":"eee", "name":"vvv", "titel":"rrr"}
                  ] 

windows.location.href = "searchResults.php?resultArray=" + JSON.stringify(resultArray);

in the php I use:

$resultArray = json_decode($_GET['resultArray']);

echo $resultArray[0]['id']; // should be "1", but show nothing

Thanks in advance for every reply!

3 Answers 3

2

json_decode will create objects for objects encoded as JSON. If you want an associative array instead, pass true as second argument:

$resultArray = json_decode($_GET['resultArray'], true);

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

1
  • why can I not choose the both solutions as the answer _, sry! Commented May 3, 2013 at 13:11
0
Its json_decode() not json.decode()

$resultArray = json_decode($_GET['resultArray']);

if you print_r($resultArray) you will get a standard class array and you can access it by echo $resultArray[0]->id and will give you 1;

1
  • Yuo are welocme @cecilfang Commented May 3, 2013 at 13:02
0

It's json_decode, not json.decode

0

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.