-2
有沒有辦法讓array_Walk_recursive返回數組的名稱而不是索引?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
)
我希望可以改變索引像第一個數組名應該是名稱,然後第二個應該是數量。另外一個問題是,我能夠推出這個名稱後,是否可以使用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
)
你的問題真的很難了解。什麼是「數組名稱」?你可以舉一個你想傳遞給'flatten'的數組的例子和你想要的結果嗎? – smarx
^輸入陣列和期望的輸出陣列所需 – 2016-08-22 04:43:58
好像這樣 [ $交易= [ 'ID'=>, 'CASE_NUMBER'=> 'TX2015-0123', '屬性'=> [ ' id'=> 69134, 'propertyType'=> [ 'id'=> 1, 'name'=>'Residential' ] ] ]; –