John Davidson

Delete all key offsets from multidimensional array in php

0 comments
Message:


In the below Array i want to remove only keys i.e [0][1][2]
How to do it.


Array
(
[0] => Array
(
[id] => 147702
[orders_id] => 832
)

[1] => Array
(
[id] => 147703
[orders_id] => 832
)

[2] => Array
(
[id] => 147704
[orders_id] => 832
)

)

My final output should be


Array
(
Array
(
[id] => 147702
[orders_id] => 832
)

Array
(
[id] => 147703
[orders_id] => 832
)

Array
(
[id] => 147704
[orders_id] => 832
)

)

Can anyone tell me, how to do this



My Answer:
You can achieve this by using a foreach loop to iterate over the array and unset the keys that you want to remove. Here is an example code snippet to achieve this:

php
$array = [
0 => [
'id' => 147702,
'orders_id' => 832
],
1 => [
'id' => 147703,
'orders_id' => 832
],
2 => [
'id' => 147704,
'orders_id' => 832
]
];

foreach ($array as &$subArray) {
unset($subArray[0]);
unset($subArray[1]);
unset($subArray[2]);
}

print_r($array);


This code will remove the keys [0], [1], and [2] from each subarray in the main array. The output will be as you desired.

Rate this post

5 of 5 based on 6752 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us