Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create new array by cutting key from an existing array over to the new array

Tags:

php

i want to take an array and cut some of the keys from it (not in order) and create a new array from them.

I have doing it using the array_shift() function, but came to a point where the next key needed to be skipped and then do the array_shift again.

How can I logically tackle this?

my array

Array
(
    [api] => Array
        (
            [0] => system
            [1] => assets
            [2] => theme
            [3] => resources
            [4] => api
            [5] => xml
            [6] => json
            [7] => jsonp
            [8] => request
        )

    [class] => Array
        (
            [name] => authentication
            [abbr] => auth
        )

    [directories] => Array
        (
            [application] => application
            [mvc] => Array
                (
                    [model] => model
                    [view] => view
                    [controller] => controller
                )

            [assets] => Array
                (
                    [folder] => assets
                    [css] => css
                    [img] => img
                    [js] => js
                )

            [config] => config
        )

    [smarty] => Array
        (
            [security] => on
            [delimiter] => Array
                (
                    [left] => {!
                    [right] => !}
                )

            [template] => Array
                (
                    [header] => header
                    [footer] => footer
                    [extension] => tpl
                )

        )

    [version] => Array
        (
            [component] => Array
                (
                    [0] => Array
                        (
                            [name] => CMS
                            [version] => 1.0
                        )

                    [1] => Array
                        (
                            [name] => TinyMCE jQuery Package
                            [version] => 3.5
                        )

                    [2] => Array
                        (
                            [name] => jQuery
                            [version] => 1.7.2
                        )

                )

            )
)

I need to make a new array from they keys: api, class, version

like image 220
Eli Avatar asked Jan 23 '26 02:01

Eli


1 Answers

Create an explicit list of keys that you'd like to move from one array to another. Cycle over that list, pulling from one and adding to another. Then remove the old copy from the original array:

// Original Array, and empty Array
$array = array( 'api' => 1, 'class' => 2, 'fizz' => 3, 'buzz' => 4 );
$newAr = array();

// For each key we'd like to keep
foreach ( array( 'api', 'class' ) as $key ) {
  // (Re)move the value from the original array to our new array
  $newAr[$key] = $array[$key]; unset( $array[$key] );
}

// Show the contents of the new array
print_r( $newAr );

Try it online now: http://codepad.org/7iYG4iVB

like image 78
Sampson Avatar answered Jan 25 '26 13:01

Sampson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!