Question

I have an array like the following . . .

Array
(
    [code] => BILL
    [assets] => Array
        (
            [en] => Array
                (
                    [labels] => Array
                        (
                            [datestamp] => April 30, 2013
                        )

                    [data] => Array
                        (
                            [Equity] => 88.83000000000
                            [Global] => 10.84000000000
                            [Other] => 0.33099095766
                        )

                )

        )

)

I have run the array_map function on this array to remove the [en] array

$en = array_map(function ($e){ return $e['en']; } , $en );

note how the resulting array has truncated the value for [code] from BILL to B

Array
(
    [code] => B
    [assets] => Array
        (
            [labels] => Array
                (
                    [datestamp] => April 30, 2013
                )

            [data] => Array
                (
                    [Equity] => 88.83000000000
                    [Global] => 10.84000000000
                    [Other] => 0.33099095766
                )

        )

)

Any tips on how to avoid that from happening. It is removing the array with the key [en] as required, but I don't want the value for [code] to be truncated.

Thanks.

Was it helpful?

Solution

You could perform type checking:

$en = array_map(function ($e){
    if (is_array($e)) {
        return $e['en'];
    } else {
        return $e;
    }
} , $en );

Although it might suffice to do just this:

$en['assets'] = $en['assets']['en'];

OTHER TIPS

Hello instead of passing entire array you mentioned as argument, you can pass the assets part of array as argument to array_map function :

$en_new = array_map(function ($e){ return $e['en']; } , $en['assets'] );

and then append the code part:

$en_new['code'] = $en['code'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top