0

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.

jimlongo
  • 365
  • 2
  • 5
  • 21
  • 2
    The index `'en'` will be cast to int (=> 0), when used to access a string by index. `BILL[0]` => `'B'` – Yoshi May 29 '13 at 17:41
  • Yes, it is treating the string like an array, Yoshi is right. –  May 29 '13 at 17:48

2 Answers2

0

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'];
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
0

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'];
jospratik
  • 1,564
  • 11
  • 19