[英]How to remove the keys from a multi-dimensional associative array?
I have an associative array which has the following format:我有一个具有以下格式的关联数组:
array (
'Shopping and fashion' =>
array (
'childData' =>
array (
'Beauty' =>
array (
'childData' =>
array (
'Cosmetics' =>
array (
'id' => '6002839660079',
'name' => 'Cosmetics',
'parentName' => 'Beauty',
),
'Tattoos' =>
array (
'id' => '6003025268985',
'name' => 'Tattoos',
'parentName' => 'Beauty',
),),))))
I want to remove the keys from the array.我想从数组中删除键。 For this I am using :
为此,我正在使用:
array_values($my_data)
However, this only works for the topmost level of array, which in this case is "Shopping and fashion".但是,这只适用于最顶层的数组,在本例中为“购物和时尚”。 It converts that to index 0. How can I do the same for every array inside the index too?
它将其转换为索引 0。我如何对索引内的每个数组也执行相同的操作?
EXPECTED OUTPUT:预期产出:
array (
0 =>
array (
'childData' =>
array (
0 =>
array (
'childData' =>
array (
0 =>
array (
'id' => '6002839660079',
'name' => 'Cosmetics',
'parentName' => 'Beauty',
),
1 =>
array (
'id' => '6003025268985',
'name' => 'Tattoos',
'parentName' => 'Beauty',
),
),
'name' => 'Beauty',
'id' => '6002867432822',
'parentName' => 'Shopping and fashion',
),)))
The array I have has many different arrays on the parent level and many more array inside them so using for each is posing a problem as the number of "childData",ie child arrays inside parent arrays are varying.我拥有的数组在父级别上有许多不同的数组,并且它们内部有更多的数组,因此使用每个数组都会带来问题,因为“childData”的数量,即父数组中的子数组是变化的。
Could recursion be possible?递归可能吗?
I don't know built-in function, but maybe this function would help you?我不知道内置函数,但也许这个函数对你有帮助?
function array_values_recursive( $array ) {
$newArray = [];
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
$newArray[] = array_values_recursive( $value );
} else {
$newArray[$key] = $value;
}
}
return $newArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.