Vicky Nehare
Vicky Nehare

Reputation: 129

add another array data in array php

hello i am trying to add another array data to an array, i have array like this,

    array:4 [▼
  "data" => array:19 [▼
    0 => array:2 [▼
      0 => array:1 [▼
        "filename" => "a"
      ]
      1 => array:1 [▼
        "filename" => "b"
      ]
    ]
    1 => array:2 [▼
      0 => array:1 [▼
        "filename" => "c"
      ]
      1 => array:1 [▼
        "filename" => "d"
      ]
    ]
    2 => array:2 [▼
      0 => array:1 [▼
        "filename" => "e"
      ]
      1 => array:1 [▼
        "filename" => "f"
      ]
    ]
    
  ]
  "video" => array:2 [▼
    0 => array:1 [▼
      "url" => "x"
    ]
    1 => array:1 [▼
      "url" => "y"
    ]
  ]
  
]

i want array like this

array:4 [▼
  "data" => array:19 [▼
    0 => array:2 [▼
      0 => array:1 [▼
        "filename" => "a",
      ]
      1 => array:1 [▼
        "filename" => "b"
      ],
      2 => array:1 [▼
        "url" => "x"
      ]
    ]
    1 => array:2 [▼
      0 => array:1 [▼
        "filename" => "c"
      ]
      1 => array:1 [▼
        "filename" => "d"
      ],
      2 => array:1 [▼
        "url" => "y"
      ]
    ]
    2 => array:2 [▼
      0 => array:1 [▼
        "filename" => "e"
      ]
      1 => array:1 [▼
        "filename" => "f"
      ],
      2 => array:1 [▼
        "url" => "x"
      ]
    ] 
  ]
]

i am trying to add array but still no result, is any way to solve this issue in php. please suggest me.

"video" data have only 2 , it will repeated , 1-2-3-1-2-3 on that array.

please suggest me how can i solve this

Upvotes: 0

Views: 89

Answers (1)

lagbox
lagbox

Reputation: 50561

You should be able to do this with array_map:

$urls = $data['video'];

end($urls);

$result = array_map(function ($i) use (&$urls) {
    $i[] = next($urls) ?: reset($urls);
    return $i;
}, $array['data']);

Upvotes: 1

Related Questions