您现在的位置是:首页
>
php 数组长度 php5.5新数组函数array
h 5.5新数组函数array PHP 发布了 其中增加了一个新的数组函数array_colum 感觉不错的!但是低版本PHP要使用 得自己实现 参考地址复制代码 代码如下: if !fu ct
php5.5新数组函数array

PHP 发布了 其中增加了一个新的数组函数array_column 感觉不错的!但是低版本PHP要使用 得自己实现 参考地址
复制代码 代码如下: if(!function_exists( array_column )){ function array_column($input $columnKey $indexKey=null){ $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false; $indexKeyIsNull = (is_null($indexKey)) ? true : false; $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false; $result = array(); foreach((array)$input as $key=>$row){ if($columnKeyIsNumber){ $tmp = array_slice($row $columnKey ); $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null; }else{ $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null; } if(!$indexKeyIsNull){ if($indexKeyIsNumber){ $key = array_slice($row $indexKey ); $key = (is_array($key) && !empty($key)) ? current($key) : null; $key = is_null($key) ? : $key; }else{ $key = isset($row[$indexKey]) ? $row[$indexKey] : ; } } $result[$key] = $tmp; } return $result; } } // 使用例子 $records = array( array( id => first_name => John last_name => Doe ) array( id => first_name => Sally last_name => Smith ) array( id => first_name => Jane last_name => Jones ) array( id => first_name => Peter last_name => Doe ) ); $firstNames = array_column($records first_name ); print_r($firstNames); $records = array( array( John Doe ) array( Sally Smith ) array( Jane Jones ) ); $lastNames = array_column($records ); print_r($lastNames); $mismatchedColumns = array( array( a => foo b => bar e => baz ) array( a => qux c => quux d => ce ) array( a => grault b => garply e => waldo ) ); $foo = array_column($mismatchedColumns a b ); print_r($foo); 复制代码 代码如下: <?php // Array representing a possible record set returned from a database $records = array( array( id => first_name => John last_name => Doe ) array( id => first_name => Sally last_name => Smith ) array( id => first_name => Jane last_name => Jones ) array( id => first_name => Peter last_name => Doe ) ); $first_names = array_column($records first_name ); print_r($first_names); ?> Array ( [ ] => John [ ] => Sally [ ] => Jane [ ] => Peter )<?php // Using the $records array from Example # $last_names = array_column($records last_name id ); print_r($last_names); ?> Array ( [ ] => Doe [ ] => Smith [ ] => Jones [ ] => Doe ) lishixinzhi/Article/program/PHP/201311/20974 很赞哦! (1050)