Home >php教程 >php手册 >Returns a specified column in an array

Returns a specified column in an array

WBOY
WBOYOriginal
2016-08-26 10:12:471037browse

Extract the same key and the value corresponding to the key from the two-dimensional array to form a new array,
PHP5.5 has been released, which adds a new array function array_column, which feels good! However, if you want to use a lower version of PHP, you have to implement it yourself, so the lower version of PHP does not support this function, so I found it in PHP and wrote it under the function of tp
//array_column — Returns a column specified in the arrayfunction arrayColumn( array $array, $column_key, $index_key=null){<br> //This function is used to check whether the specified function has been defined. The parameter function_name is the name of the function to be checked. If the specified function has been defined, it returns a true value, otherwise it returns a false value. <br> If(function_exists('array_column')){<br>                  return array_column($array, $column_key, $index_key);<br>          }<br>          $result = array();<br> foreach($array as $arr){<br>                   if(!is_array($arr)) continue;<br> <br> If(is_null($column_key)){<br>                 $value = $arr;<br>                  }else{<br>                   $value = $arr[$column_key];<br>             }<br> <br> If(!is_null($index_key)){<br>                  $key = $arr[$index_key];<br>                   $result[$key] = $value;<br>                  }else{<br>                   $result[] = $value;<br>             }<br> <br>          }<br> <br> Return $ result; <br> }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn