English 中文(简体)
创建新的阵列,将现有阵列至新阵列的关键加以削减
原标题:create new array by cutting key from an existing array over to the new array
  • 时间:2012-05-08 04:02:37
  •  标签:
  • 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.

我利用了<代码>array_second()功能这样做,但到了下一个关键需要ski熟的时候,然后又做临时安排。

How can I logically tackle this?

我的阵容

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
                        )

                )

            )
)

我需要从这些钥匙中选取一个新阵列:api,class,

最佳回答

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

问题回答

如果这只是三个关键因素,你需要:

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

array_slice() 允许你把一部分阵列lic。 这一部分可以是单一要素,也可以是一系列要素。 基本上,切片功能让你在像子体这样的阵列上 n颈,让你打上str。

这似乎过于简单:

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

然后是var_dump (new_array);,看它是否载有预期产出。

$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 - 此处缩短

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




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...