Create a new array by cutting a key from an existing array into a new array

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

I do this using a function array_shift(), but have reached the point where I had to skip the next key and then execute array_shift again.

How can I deal with this logically?

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 of these keys: api, class,version

+3
source share
5 answers

Create an explicit list of keys that you want to move from one array to another. Go through this list, pulling it from one and adding to the other. 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 );

Now online: http://codepad.org/7iYG4iVB

+1

3 :

$newArray = array(
    "api" => $oldArray["api"],
    "class" => $oldArray["class"],
    "version" => $oldArray["version"]
);
+1

Here is array_slice () , which allows you to "cut out" part of the array. This part may be one element or a series of elements. Basically, the slice function allows you to hack "n chop" on an array such as substr (), allowing you to hack an n string into a string.

0
source
$keys = array(1, 5, 'foo');

$new = array();
foreach ($keys as $key) {
    $new[$key] = $old[$key];
    // or maybe just $new[] = $old[$key];
    unset($old[$key]);
}

There are shorter ways to do this, but I think you can understand how this works.

edit - Here is a shorter path

$keys = array(1, 5);
$new = array_intersect_key($old, array_flip($keys)));
$old = array_diff_key($old, array_flip($keys)));
0
source

This seems too simple:

$new_array = array( 
    'api' => $old_array['api'],
    'class' => $old_array['class'],
    'version' => $old_array['version']
);

Then do a var_dump( $new_array);to see if it contains the desired result.

0
source

All Articles