-1

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" },
    ]
];
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Shahrad Elahi
  • 774
  • 12
  • 22
  • 2
    Does this answer your question? [Is it possible to add dynamically named properties to JavaScript object?](https://stackoverflow.com/questions/1184123/is-it-possible-to-add-dynamically-named-properties-to-javascript-object) – Tân Dec 26 '19 at 08:48
  • @Tân this can't help me, I want to add another JSON inside double JSON array. – Shahrad Elahi Dec 26 '19 at 08:51
  • I've given you the way to *add* it. Please read the answer carefully. – Tân Dec 26 '19 at 08:54
  • 1
    @Tân It seems Shardad needs PHP solution, not that. – Kamran Dec 26 '19 at 08:58

2 Answers2

3
$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)
Kamran
  • 523
  • 6
  • 18
0

You need to first handle your data in a array and then convert it to a json array

I would do it like this :

$array = [
    [
        [
            "text" => "Row 1 Column 1"
        ]
    ],
    [
        [
            "text" => "Row 2 Column 1"
        ],
        [
            "text" => "Row 2 Column 2"
        ]
    ]
];

// a element in the first sub array
$array[0][] = [
    "text" => "Row 1 Column 2"
];

Does that help ?

Michaël
  • 1,120
  • 1
  • 15
  • 30