<?php
$result = [];
$init_arr = [
['name'=>'grape','num'=>2],
['name'=>'melon','num'=>1]
];
$result['init_arr'] = $init_arr;
$removed_arr = my_remove_data( $init_arr, 2 );
$result['removed_arr'] = $removed_arr;
array_unshift( $init_arr, ['name'=>'apple','num'=>3] );
$result['added_arr'] = $init_arr;
var_dump( $result );
function my_remove_data( $arr, $id ){
foreach($arr as $k1=>$a) {
foreach($a as $k2=>$b) {
if($b == $id) {
unset($arr[$k1]);
}
}
}
return $arr;
}
- Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- array(3) {
["init_arr"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(5) "grape"
["num"]=>
int(2)
}
[1]=>
array(2) {
["name"]=>
string(5) "melon"
["num"]=>
int(1)
}
}
["removed_arr"]=>
array(1) {
[1]=>
array(2) {
["name"]=>
string(5) "melon"
["num"]=>
int(1)
}
}
["added_arr"]=>
array(3) {
[0]=>
array(2) {
["name"]=>
string(5) "apple"
["num"]=>
int(3)
}
[1]=>
array(2) {
["name"]=>
string(5) "grape"
["num"]=>
int(2)
}
[2]=>
array(2) {
["name"]=>
string(5) "melon"
["num"]=>
int(1)
}
}
}
preferences:
82.59 ms | 409 KiB | 5 Q