Jason Lee Jason Lee - 3 months ago 12
PHP Question

array_walk_recursive to return array name rather than index number

is there a way for the array_Walk_recursive to return the name of the array instead of index?

function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}


my result is something like this

Array
(
[0] => 888
[1] => TY7452
[2] => 63214
[3] => 0
[4] => Upper
)


i hope can change the indexes to the array name like the first one should be Name then second should be number. And guys one more question is after i able to project out the name, is it possible for me to use implode to some sort of like set a path of the array name which will replace the index number that im currently getting? eg, car.model.number

my array

$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
]
]
],
];


Desired Result

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] => 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
)

Answer

Give this a try:

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
// )