0

I'm sending an ajax request from js to php.

In the php code I create some 2-D array like this:

$arr = array();
for ($i=1; $i<=100; $i++){
    $array[$i][0] = rand(0,100000);
    $array[$i][1] = rand(0,100000);
    $array[$i][2] = rand(0,100000);
}

header("Content-Type: application/json", true);

echo json_encode($CalcTable);

exit;

On the js file, I get the data parameter (the parameter that returned from the ajax done function) like an object that contains 100 arrays inside.

I want to convert this returned object to a js array that contains all the 100 arrays inside it (and all each array contains the 3 arrays with the random values).

Thanks!

3
  • it's json. it already IS javascript. assuming you're doing this via jquery, and a $.getJSON() call, then it's already been decoded into a native js array and you just start using it. Commented Feb 3, 2016 at 21:32
  • What have you tried? What challenges are you facing? We're happy to help, but we're not going to write the program for you. Commented Feb 3, 2016 at 21:33
  • I dont want you to write the program for me............................... I just want you to help me solve my problem... As I wrote I get back an object that contains 100 arrays. I want to store this object inside other array. On chrome "watch" area I stored it on the array and saw it like an object and not like an ragular array... Commented Feb 4, 2016 at 19:06

1 Answer 1

1

The problem here is that you are are starting your array at index 1 instead of 0. When encoding this, PHP "converts" into an object, because arrays must start at 0.

To fix, this you need to create an array of your 3 values, then push it into the main array.

for ($i=0; $i<100; $i++){
    $array[] = array(rand(0,100000), rand(0,100000), rand(0,100000));
}
Sign up to request clarification or add additional context in comments.

Comments

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.