有没有办法让array_Walk_recursive返回数组的名称而不是索引?
function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
我的结果是这样的
Array
(
[0] => 888
[1] => TY7452
[2] => 63214
[3] => 0
[4] => Upper
)
我希望可以将索引更改为数组名称,如第一个应该是Name,然后第二个应该是数字。还有一个问题是在我能够突出显示名称后,我是否可以使用implode来设置数组名称的路径,这将替换我当前获得的索引号?例如,car.model.number
我的数组
$trading = [
'id' => 888,
'case_number' => 'KO2017-987',
'property' => [
'id' => 78563,
'propertyType' => [
'id' => 1,
'name' => 'Residential'
],
'address' => [
'block' => '85',
'street' => 'Jalan Serjana',
'subpremise' => '#07-05',
'building' => 'TM Block',
'country_code' => 'MY'
],
'askingPrice' => '650000.00',
'photos' => [
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg',
'is_default' => 1
],
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg',
'is_default' => 0
],
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg',
'is_default' => 0
]
]
],
];
期望的结果
阵列
(
[id] => 888
[case_number] => KO2017-987
[property.id] => 78563
[property.propertyType.id] => 1
[property.propertyType.name] => Residential
[property.address.block] => 85
[property.address.street] => Jalan Serjana
[property.address.subpremise] => #07-05
[property.address,building] => TM Block
[property.address.country_code] => MY
[property.askingPrice] => 6500000.00
[property.photos.0.url] => https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg
[property.photos.o.is_default] => 1
[property.photos.1.url] => https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg
[property.photos.1.is_default] => 0
[property.photos.2.url] => https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg
[property.photos.2.is_default] => 0
)
答案 0 :(得分:0)
尝试一下:
function flatten(array $array, $prefix="") {
$result = Array();
array_walk($array, function ($value, $key) use ($array, $prefix, &$result) {
$path = $prefix ? "$prefix.$key" : $key;
if (is_array($value)) {
$result = array_merge($result, flatten($value, $path));
} else {
$result[$path] = $value;
}
});
return $result;
}
$trading = [
'id' => 888,
'case_number' => 'KO2017-987',
'property' => [
'id' => 78563,
'propertyType' => [
'id' => 1,
'name' => 'Residential'
],
'address' => [
'block' => '85',
'street' => 'Jalan Serjana',
'subpremise' => '#07-05',
'building' => 'TM Block',
'country_code' => 'MY'
],
'askingPrice' => '650000.00',
'photos' => [
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg',
'is_default' => 1
],
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg',
'is_default' => 0
],
[
'url' => 'https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg',
'is_default' => 0
]
]
],
];
print_r(flatten($trading));
// Output:
// Array
// (
// [id] => 888
// [case_number] => KO2017-987
// [property.id] => 78563
// [property.propertyType.id] => 1
// [property.propertyType.name] => Residential
// [property.address.block] => 85
// [property.address.street] => Jalan Serjana
// [property.address.subpremise] => #07-05
// [property.address.building] => TM Block
// [property.address.country_code] => MY
// [property.askingPrice] => 650000.00
// [property.photos.0.url] => https://www.jokok.com/thumbnails/600x400F/1k985k63-652b-4dpc-988b-b98f75364db0.jpg
// [property.photos.0.is_default] => 1
// [property.photos.1.url] => https://www.jokok.com/thumbnails/600x400F/8cf78fb6-9545-4f5f-8dfc-235a57a2b8c1.jpg
// [property.photos.1.is_default] => 0
// [property.photos.2.url] => https://www.jokok.com/thumbnails/600x400F/e456218f-8b22-4250-9b29-72c1d3f5dc45.jpg
// [property.photos.2.is_default] => 0
// )