3

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

3
  • Can you give us an example. Is there a definable criteria that determines which elements you're taking, and which ones you're leaving? Commented May 8, 2012 at 4:03
  • Post some concrete examples / parts of your code, this will help us provide a meaningful solution. Commented May 8, 2012 at 4:03
  • What do you want to copy from the mentioned array? Commented May 8, 2012 at 4:06

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

If it's just those 3 keys you need:

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

6 Comments

but i need to cut then out of the existing array
$oldArray would be your existing array. $newArray would contain only those 3 keys and the contents of the old array.
I don't know who's downvoting and not defending their vote, but they downvoted the correct answers. So have an upvote. Clearly this answer is better than looping over the array, as it runs in constant time, as opposed to O(n).
I didn't downvote, however this solution leaves the values within the original array. If I'm not mistaken, the OP wanted them removed from the original array.
You can always overwrite the original with the new afterwards... (I know you probably know that, just saying)
|
0

There's array_slice() which lets you 'slice' out a portion of an array. That portion can be a single element, or a range of elements. Basically the slice function lets you hack 'n chop on an array like substr() lets you hack 'n chop up a string.

1 Comment

seems to be close to what i want, i just don't understand the logic on how to go about using the array_splice
0

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 output.

Comments

0
$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's shorter ways to do this, but I think you can understand how this works.

edit - Here's a shorter way

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

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.