array_column_PHP array_column()函数与示例

array_column

PHP array_column()函数 (PHP array_column() function)

array_column() function is an array function, it is used to get the value from a single column of given single dimensional, multiple dimensional arrays, object etc. By using this function, we can also specify the other column's value as the "keys" of the new returned array.

array_column()函数是一个数组函数,用于从给定的一维,多维数组,对象等的单列中获取值。通过使用此函数,我们还可以将另一列的值指定为“键”返回的新数组。

Syntax:

句法:

    array_column(array_name, column_name, [index_key]) : array

Here,

这里,

  • array_name is an input array/main array from where we have to extract the column's value.

    array_name是输入数组/主数组,我们必须从中提取列的值。

  • column_name is the name of the column of the input array.

    column_name是输入数组的列的名称。

  • index_key is an optional parameter, it is used to define the values of another column as index keys in a returned array.

    index_key是一个可选参数,用于将另一列的值定义为返回数组中的索引键。

It returns an array with keys (either integer index or other column’s name as index keys) & value.

它返回一个带有键(整数索引或其他列名作为索引键)和值的数组。

Examples:

例子:

    Input:
	    $employee = array(
		    array(
			    'emp_id' => 101,
			    'name' => "Amit",
			    'city' => "Gwalior",
		    ),
		    array(
			    'emp_id' => 102,
			    'name' => "Mohan",
			    'city' => "New Delhi",
		    ),
		    array(
			    'emp_id' => 103,
			    'name' => "Mohit",
			    'city' => "Chennai",
		    ),		
	    );

    Function call: array_column($employee, 'name');
    Output:
    Array
    (
        [0] => Amit 
        [1] => Mohan
        [2] => Mohit
    )

PHP code:

PHP代码:

<?php
	$employee = array(
		array(
			'emp_id' => 101,
			'name' => "Amit",
			'city' => "Gwalior",
		),
		array(
			'emp_id' => 102,
			'name' => "Mohan",
			'city' => "New Delhi",
		),
		array(
			'emp_id' => 103,
			'name' => "Mohit",
			'city' => "Chennai",
		),		
	);
	
	//Extracting the values of "name"
	$arr1 = array_column($employee, 'name');
	print_r ($arr1);
	//Extracting city with index key as "emp_id"
	$arr1 = array_column($employee, 'city', 'emp_id');
	print_r ($arr1);	
	//Extracting name with index key as "name"
	$arr1 = array_column($employee, 'city', 'name');
	print_r ($arr1);		
?>

Output

输出量

Array
(
    [0] => Amit 
    [1] => Mohan
    [2] => Mohit
)
Array
(
    [101] => Gwalior
    [102] => New Delhi
    [103] => Chennai
)
Array
(
    [Amit] => Gwalior
    [Mohan] => New Delhi
    [Mohit] => Chennai
)


翻译自: https://www.includehelp.com/php/array_column-function-with-example.aspx

array_column

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值