Skip to content
Advertisement

Pushing a sub array into the same array

I am trying to put content of one array into the same array. Here I have an array $mclass with values such as

JavaScript

You can see I have room_id index with 1,3,5 value. Now, I want to explode the room_id and get duplicate of same array index data with change of room_id and push into the array. and finally delete the current array index such as [0]. Here I want the final result as.

JavaScript

Here is my code for the same:

JavaScript

Advertisement

Answer

The code below does what you’re looking for using only arrays. So you’ll have to change the array access operators to -> since you’re accessing an object. I’d do so, but it would break the example, so I’ll leave that up to you.

Code Explained: Loop through array selecting each subarray (object in your case), explode on the $item('room_id') … ($item->room_id in your case) … and create sub arrays, via loop, from that using the data from the original using each key. Remove the original item (which has the combined room_ids) and combine the placeholder and original array.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement