2021-06-29

Generating multilevel associative array using php loop

Trying to generate array for json output from a nested php loop, any suggestions on how to accomplish this?

Here is the current code am I using

$v_id=0;
$x=0;
$item[] = array();
foreach ($abc as $vm) {
    foreach ($vm->activity as $record) {

        $item['instruction']              = $record->step;
        $item['id']              = $x;
        $id++;

    } 
    $v_id++;
}

However each loop of the main loop statement is overrating the previous entries

This is the desired output

steps": [
        {
            "v_id": 0,
            "instruction_steps": [
                {
                    "id": 0,
                    "instruction": "<p>Select something </p>",
                  
                   
                {
                    "id": 1,
                    "instruction": "<p>Do something</p>",
                   
                }
            ]
        },


from Recent Questions - Stack Overflow https://ift.tt/3dqjIBs
https://ift.tt/eA8V8J

No comments:

Post a Comment