0

I'm trying to return two arrays from a PHP with a JSON object.

Here's my PHP code:

$return = array();
$return += array('val1' => '1',
                 'val2' => '2',
                 'val3' => '3');
$tab = array();                 
for($i=0; $i<3; $i++)
{
    $val = "table1 " . $i;
    array_push($tab, array('tab1' => $val));
}
$return += $tab;

$tab = array();
for ($i = 0; $i < 5; $i++) {
    $val = "table2 " . $i;
    array_push($tab, array('tab2' => $val));
}
$return += $tab;
echo json_encode($return);

and here's my JS code:

console.log("val1=" + data.val1);
console.log("val2=" + data.val2);
console.log("val3=" + data.val3);
for(var i=0; i<3; i++)
console.log("tab1_" + i + "=" + data[i].tab1);
for (var i = 0; i < 5; i++)
console.log("tab2_" + i + "=" + data[i].tab2);

And here's what I get on console:

val1=1
val2=2
val3=3
tab1_0=table1 0
tab1_1=table1 1
tab1_2=table1 2
tab2_0=undefined
tab2_1=undefined
tab2_2=undefined
tab2_3=undefined
tab2_4=undefined

Why can't I add to arrays to the JSON object? What am I doing wrong? Thank you for your help.

5
  • 3
    Have you tried to print_r($return); to see if your array holds the expected data? Commented Dec 18, 2018 at 11:09
  • Look at the print_r() and you will see the array you create is not what you think it is Commented Dec 18, 2018 at 11:13
  • To match the array you are creating change for (var i=0;i<5;i++) to for (var i=3;i< 8;i++) Commented Dec 18, 2018 at 11:16
  • @kerbholz your answer helped solve my problem! Thank you! Commented Dec 18, 2018 at 11:23
  • @RiggsFolly your answer helped solve my problem! Thank you! Commented Dec 18, 2018 at 11:23

1 Answer 1

1

The + and += operators often don't work as expected when combining arrays. If the keys in the second array are already present in the first array, they will be skipped. Your two $tab arrays both had keys 0, 1, and 2 so they weren't being added. Instead, change

$return += $tab;

to

$return = array_merge($return, $tab);

which should finally be something like this:

$return = array();
$return += array('val1' => '1',
                'val2' => '2',
                'val3' => '3');

$tab = array();
for($i=0; $i<3; $i++)
{
    $val = "table1 " . $i;
    array_push($tab, array('tab1' => $val));
}
$return = array_merge($return, $tab);

$tab = array();
for ($i = 0; $i < 5; $i++) {
    $val = "table2 " . $i;
    array_push($tab, array('tab2' => $val));
}
$return = array_merge($return, $tab);

echo json_encode($return);

and the result should be what you are expecting:

    {
    "0": {
        "tab1": "table1 0"
    },
    "1": {
        "tab1": "table1 1"
    },
    "2": {
        "tab1": "table1 2"
    },
    "3": {
        "tab2": "table2 0"
    },
    "4": {
        "tab2": "table2 1"
    },
    "5": {
        "tab2": "table2 2"
    },
    "6": {
        "tab2": "table2 3"
    },
    "7": {
        "tab2": "table2 4"
    },
    "val1": "1",
    "val2": "2",
    "val3": "3"
}
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.