I have an array like below:
array:3 [▼
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso"
"room_id" => array:3 [▼
0 => "089a6ca8-26de-3ca2-bbbf-17c410d15266"
1 => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c"
2 => "2d9b7392-aa50-37e4-882a-804affb87223"
]
"new_electric" => array:3 [▼
0 => "123"
1 => "234"
2 => "345"
]
]
How can I slice it into new array which like
array:4 [▼
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso"
"1" => array:2 [▼
"room_d" => "089a6ca8-26de-3ca2-bbbf-17c410d15266"
"new_electric => "123"
]
"2" => array:2 [▼
"room_id" => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c"
"new_electric" => "234"
]
"3" => array:2 [▼
"room_id" => "2d9b7392-aa50-37e4-882a-804affb87223"
"new_electric" => "345"
]
]
I think I need to use an array_push
but I don't know how to do that.
You have to use foreach
$arr = array(
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso",
"room_id" => array(
0 => "089a6ca8-26de-3ca2-bbbf-17c410d15266",
1 => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c",
2 => "2d9b7392-aa50-37e4-882a-804affb87223",
),
"new_electric" => array(
0 => "123",
1 => "234",
2 => "345",
)
);
$final = array();
$final[ "_token" ] = $arr[ "_token" ];
foreach( $arr[ "room_id" ] as $key => $value ) {
$final[ ( $key + 1 ) ] = array(
"room_id" => $value,
"new_electric" => $arr[ "new_electric" ][ $key ]
);
}
This will result to:
Array
(
[_token] => hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso
[1] => Array
(
[room_id] => 089a6ca8-26de-3ca2-bbbf-17c410d15266
[new_electric] => 123
)
[2] => Array
(
[room_id] => 13c56ac2-6c77-3ca7-b24a-2a70f4f0526c
[new_electric] => 234
)
[3] => Array
(
[room_id] => 2d9b7392-aa50-37e4-882a-804affb87223
[new_electric] => 345
)
)
Note: Use $key + 1
because you want the index to start from 1