-1

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
)
Jason Lee
  • 43
  • 9
  • 3
    Your question is really hard to understand. What is an "array name"? Can you give an example of an array you want to pass to `flatten` and the result you want? – user94559 Aug 22 '16 at 04:43
  • 2
    ^input array and desired output array required –  Aug 22 '16 at 04:43
  • okay something like this [ $trading = [ 'id' => , 'case_number' => 'TX2015-0123', 'property' => [ 'id' => 69134, 'propertyType' => [ 'id' => 1, 'name' => 'Residential' ] ] ]; – Jason Lee Aug 22 '16 at 04:47
  • Alright, I gave up and voted to close. If you try again, please include an actual running example in your question, including data. Then give the exact result you want to get. – user94559 Aug 22 '16 at 04:52
  • (The array you shared isn't valid PHP syntax, and it doesn't tell us what you want.) – user94559 Aug 22 '16 at 04:53
  • hi there sorry i have included my array – Jason Lee Aug 22 '16 at 04:54
  • And... what do you want to do? What result do you want? – user94559 Aug 22 '16 at 04:56
  • the result i wanted is like this Array ( [id] => 888 [case_number] => KO2017-987 [property.id] => 78563 [property.propertyType.id] => 1 [property.propertyType.name] => Residential ) – Jason Lee Aug 22 '16 at 04:59
  • Possible duplicate of [PHP - Convert multidimensional array to 2D array with dot notation keys](http://stackoverflow.com/questions/10424335/php-convert-multidimensional-array-to-2d-array-with-dot-notation-keys) – trincot Aug 22 '16 at 05:05

1 Answers1

2

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
// )
user94559
  • 59,196
  • 6
  • 103
  • 103