Php 向不带';数组中没有键
给定空闲数组Php 向不带';数组中没有键,php,arrays,sorting,Php,Arrays,Sorting,给定空闲数组$array: Array ( [1] => Array ( [0] => 267 [location_id_keep] => 261 ) [2] => Array ( [0] => 266 [location_id_keep] => 262 ) [3] => Array ( [0] => 2669
$array
:
Array
(
[1] => Array
(
[0] => 267
[location_id_keep] => 261
)
[2] => Array
(
[0] => 266
[location_id_keep] => 262
)
[3] => Array
(
[0] => 2669
[1] => 2670
[location_id_keep] => 2668
)
[4] => Array
(
[0] => 266
[1] => 2670
[location_id_keep] => 2668
)
)
我想将没有[location\u id\u keep
]键的值添加到每个数组中,然后使用fallowing键:[location\u id\u delete
]以便获得以下输出:
Array
(
[1] => Array
(
[location_id_delete] => 267
[location_id_keep] => 261
)
[2] => Array
(
[location_id_delete] => 266
[location_id_keep] => 262
)
[3] => Array
(
[location_id_delete] => [2669, 2670]
[location_id_keep] => 2668
)
[4] => Array
(
[location_id_delete] => [266, 2670]
[location_id_keep] => 2668
)
)
是否有任何方法可以在分配给没有键的现有值的数组中添加键?这应该可以,但我建议您重建数据的结构 您可以尝试使用和。array_filter用于过滤键为number的值(您的意思是
location_id_delete
)
正在工作。这是片段
array_walk($arr, function (&$v) {
// keeping location_id_keep aside to perform operation of replacing keys
$temp = array_diff_key($v, ['location_id_keep' => '']);
if (!empty($temp)) {
// getting the count of sub arrays other than location_id_keep
$cnt = count($temp);
foreach ($temp as $key => $value) {
// if one record then directly assign data
if ($cnt == 1) {
$v['location_id_delete'] = $value;
} else{ // assign multiple data in an arrat
$v['location_id_delete'][] = $value;
}
// unset numeric index or other than location_id_keep
unset($v[$key]);
}
}
});
-将用户提供的函数应用于阵列的每个成员-使用用于比较的键计算数组的差异 输出:-
Array
(
[1] => Array
(
[location_id_keep] => 261
[location_id_delete] => 267
)
[2] => Array
(
[location_id_keep] => 262
[location_id_delete] => 266
)
[3] => Array
(
[location_id_keep] => 2668
[location_id_delete] => Array
(
[0] => 2669
[1] => 2670
)
)
[4] => Array
(
[location_id_keep] => 2668
[location_id_delete] => Array
(
[0] => 266
[1] => 2670
)
)
)
这将起作用
foreach($newArray as $key => $val){
if(array_key_exists('location_id_keep',$val)){
$array[$key]['location_id_keep'] = $val['location_id_keep'];
unset($val['location_id_keep']);
}
$count = count($val);
if($count>1){
$array[$key]['location_id_delete'] = implode(',',$val);
}else{
$array[$key]['location_id_delete'] = $val[0];
}
}
输出
Array
(
[1] => Array
(
[location_id_keep] => 261
[location_id_delete] => 267
)
[2] => Array
(
[location_id_keep] => 262
[location_id_delete] => 266
)
[3] => Array
(
[location_id_keep] => 2668
[location_id_delete] => 2669,2670
)
[4] => Array
(
[location_id_keep] => 2668
[location_id_delete] => 266,2670
)
)
功能溶液
foreach($arrays as &$array){
// get numeric key items
$temp = array_filter($array, 'is_numeric',ARRAY_FILTER_USE_KEY);
// remove them from array and return under new key
$array = array_diff($array, $temp) + ['location_id_delete'=>$temp];
}
您可以将
foreach
与内爆
和取消设置
foreach($a as $k => &$v){
$location = $v['location_id_keep'];
unset($v['location_id_keep']);
$r[] = [
'location_id_delete' => implode(',',$v),
'location_id_keep' => $location
];
}
实时示例:您希望如何使用相同的密钥
位置\u id\u delete
?您想要的输出无效;PHP数组不能有多个具有相同键的元素。其错误[location\u id\u delete]=>2669[location\u id\u delete]=>2670
应该是,例如,[location\u id\u delete]=>[26692670]
类似这样的[location\u id\u delete]=>[266922670]etcI对我所需的输出进行了更正。
foreach($arrays as &$array){
// get numeric key items
$temp = array_filter($array, 'is_numeric',ARRAY_FILTER_USE_KEY);
// remove them from array and return under new key
$array = array_diff($array, $temp) + ['location_id_delete'=>$temp];
}
foreach($a as $k => &$v){
$location = $v['location_id_keep'];
unset($v['location_id_keep']);
$r[] = [
'location_id_delete' => implode(',',$v),
'location_id_keep' => $location
];
}