标签:size php版本 records val first amp div var_dump dump
例如将如下二位数组转以为以为一维数组
$records = [
[
‘id‘ => 2135,
‘first_name‘ => ‘John‘,
‘last_name‘ => ‘Doe‘,
],
[
‘id‘ => 3245,
‘first_name‘ => ‘Sally‘,
‘last_name‘ => ‘Smith‘,
],
[
‘id‘ => 5342,
‘first_name‘ => ‘Jane‘,
‘last_name‘ => ‘Jones‘,
],
[
‘id‘ => 5623,
‘first_name‘ => ‘Peter‘,
‘last_name‘ => ‘Doe‘,
]
];
array_column()是PHP内置函数,限制是PHP版本必须是5.5.0及以上版本!
<?php
$first_names = array_column($records, ‘first_name‘);
var_dump($first_names);
?>
打印结果为:
$first_names = [‘John‘,‘Sally‘,‘Jane‘,‘Peter‘];
<?php $first_names = array_column($records, ‘first_name‘,‘id‘); var_dump($first_names); ?>
打印结果为:
$first_names = [2135 =>‘John‘,3245 => ‘Sally‘,5342 => ‘Jane‘,5623 => ‘Peter‘];
array_walk()函数使用用户自定义函数对数组中的每个元素做回调处理,实现当前功能的方法:
$first_names= []; array_walk($records, function($value, $key) use (&$first_names){ $first_names[] = $value[‘first_name‘]; });
var_dump($first_names);
打印结果为:
$first_names = [‘John‘,‘Sally‘,‘Jane‘,‘Peter‘];
array_map()函数和array_walk() 作用类似,将回调函数作用到给定数组的单元上。
$first_names= [];
array_map(function($value) use (&$first_names){
$first_names[] = $value[‘first_name‘];
}, $records);
var_dump($first_names);
打印结果为:
$first_names = [‘John‘,‘Sally‘,‘Jane‘,‘Peter‘];
array_reduce — 用回调函数迭代地将数组简化为单一的值。
标签:size php版本 records val first amp div var_dump dump
原文地址:https://www.cnblogs.com/honeyJYY/p/10430716.html