array_walk_recursive 到 return 数组名而不是索引号
array_walk_recursive to return array name rather than index number
有没有办法让 array_Walk_recursive 以 return 数组名称代替索引?
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
)
我希望可以将索引更改为数组名称,例如第一个应该是名称然后第二个应该是数字。伙计们还有一个问题是在我能够预测出名称之后,我是否可以使用 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
)
试一试:
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
// )
有没有办法让 array_Walk_recursive 以 return 数组名称代替索引?
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
)
我希望可以将索引更改为数组名称,例如第一个应该是名称然后第二个应该是数字。伙计们还有一个问题是在我能够预测出名称之后,我是否可以使用 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
)
试一试:
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
// )