Skip to content
Advertisement

How to push an object into another nested array [closed]

I’m trying to push a row to my array. I have an array that looks like this:

$array = [
    [
        { "id": 1, "name": "John" },
        // Another object
    ],
    [
        { "id": 1, "name": "Jeff" },
        { "id": 2, "name": "Jane" },
    ]
];

Advertisement

Answer

$json= '
    [
        [
            {
                "text":"Row 1 Column 1"
            }
        ],
        [
            {
                "text":"Row 2 Column 1"
            },
            {
                "text":"Row 2 Column 2"
            }
        ]
    ]
    ';

    $p = json_decode($j);

    $p[0][]=["text"=>"Row 1 Column 2"];

    print_r(json_encode($p)); // print_r for debug, $result = json_encode($p)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement