1

I've researched topics similar to this but not exactly what I'm looking to do. I have a multidimensional array like the following.

[code]  =>  BILL
[assets] => Array
    (
        [en] => Array
            (
                [datatype] => My Assets
                [data] => Array
                    (
                        [Corporate Equity] => 41
                        [Global Equity] => 24
                        [Fixed Income – Government] => 22
                        [Fixed Income – Corporate] => 8.1
                        [Other] => 3.57
                    )

            )

    )

I'd like to remove the first inner array, but preserve the values. Shift them up one level in the array so that it looks like this.

[code]   => BILL
[assets] => Array
    (

        [datatype] => My Assets
        [data] => Array
            (
                [Corporate Equity] => 41
                [Global Equity] => 24
                [Fixed Income – Government] => 22
                [Fixed Income – Corporate] => 8.1
                [Other] => 3.57
            )


    )

This is just the beginning of the array, there are other instances of the same key [en] at the same level.

I've tried unset, array_shift and others but I need to keep the contents of [en], just shift them up one level in the array.

1
  • 4
    there are other instances of the same key [en] at the same level How will you deal with the other instances? If you shift them all up, won't you overwrite each previous instance? Commented May 15, 2013 at 20:47

4 Answers 4

5

You can use array_map which returns an array which contains all elements of the previous array after applying the function.

In this case it will simply take the array at index en and add it's contents to the new array.

http://php.net/manual/en/function.array-map.php

$arr = array('assets' => array(
    'en' => array(
        'datatype' => 'My Assets',
        'data' => array(
            'Corporate Equity' => 41,
            'Global Equity' => 24,
             'Fixed Income – Government' => 22,
             'Fixed Income – Corporate' => 8.1,
             'Other' => 3.57
        )
    )
));

$new_arr = array_map(function ($e) {
    return $e['en'];
}, $arr);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works . . . but I hadn't fully enough described the array. I've edited my original array as you can see there is a pair [code]=>BILL at the top level and after the array_map filter is applied the result is that [code]=>B. Why would array_map affect that value?
0

A simple solution that assumes the key to always be en and the subkeys to always be (only) datatype and data:

$assets['datatype'] = $assets['en']['datatype'];
$assets['data'] = $assets['en']['data'];
unset( $assets['en'] );

This code could be problematic for you in the future if that array structure ever changes (it lacks extensibility), but it gets you what you want given the information you have provided.

Comments

0

array_shift is the opposite of array_pop. Used in stack/queue like structures for removing the fist element http://php.net/manual/en/function.array-shift.php

What you want to do is flatten the array. But if you want to keep all the other sub-arrays as you mentioned, you might look up array_merge.

Comments

0

I faced the same scenario after using reader to read xml file, the returned array was having inserted 0 key array in each level like the following one:


'config' =>
           0 => 
               'products' =>
                            0 =>
                                'media' => 
                                 .
                                 .
                                 .

so I built a small function to get rid of a specific key and shift up its child's in a two dimensions array, in my case the key was 0. hopping this would help somebody also.


public function clearMaps(&$maps, $readerMaps, $omittedKey)
{
    if (is_array($readerMaps)) {
        foreach ($readerMaps as $key => $map) {

            if ($key !== $omittedKey) {

                $maps[$key] = [];
                $this->clearMaps($maps[$key], $readerMaps[$key], $omittedKey);
            } else {

                $this->clearMaps($maps, $readerMaps[$key], $omittedKey);
            }
        }
    } else {

        $maps = $readerMaps;
    }
}

// $maps: cleaned array, will start as empty array
// $readerMaps: array needs to be cleaned
// $omittedKey: array key to git rid of.
// first call is clearMaps([], $readerMaps, 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.