Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用当前数组中的项创建新数组_Php_Arrays - Fatal编程技术网

Php 使用当前数组中的项创建新数组

Php 使用当前数组中的项创建新数组,php,arrays,Php,Arrays,我有一个数组,看起来像这样 Array ( [result] => Success [finals] => Array ( [0] => Array ( [id] => 633 [name] => RESULT84 ) [0] =>

我有一个数组,看起来像这样

Array
(
    [result] => Success
    [finals] => Array
        (
            [0] => Array
                (
                    [id] => 633
                    [name] => RESULT84
                )
                [0] => Array
                (
                    [id] => 766
                    [name] => RESULT2
                )
                [0] => Array
                (
                    [id] => 22
                    [name] => RESULT1
                )
        )
)
使用PHP,我试图从中创建另一个只包含[name]字段的数组

我是否需要从中创建新数组,或者是否有方法按原样使用它?

使用,假设您的数组位于名为
$data
的变量中:

$names = array_column($data['finals'], 'name');

print_r($names);
收益率:

Array
(
    [0] => RESULT84
    [1] => RESULT2
    [2] => RESULT1
)
array\u column()
可从PHP 5.5.0版获得

您也可以使用:


希望这对您有所帮助:)

如果您需要单数组中的名称,您可以声明一个新数组
$array=[]
和在foreach中使用array_push全局函数并执行
array_push($array,$value['name'])
$names = array_map(function ($final) {
    return $final['name'];
}, $data['finals']);