Home  >  Article  >  Backend Development  >  PHP lets the arrays with the same values ​​form a new array instance to explain

PHP lets the arrays with the same values ​​form a new array instance to explain

jacklove
jackloveOriginal
2018-07-03 18:04:421804browse

The editor below will share with you an article in PHP to form a new array instance with the same value in the array. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

The example is as follows:

$arr = array( 
  0=>array('key1'=>'value1' , 'key2'=>'value2'), 
  1=>array('key1'=>'value1' , 'key2'=>'value3'), 
  2=>array('key1'=>'value2' , 'key2'=>'value4'), 
  999=>array('key1'=>'value2' , 'key2'=>'value5') 
); 
 
$result = array(); 
<span style="color:#FF0000;">foreach ($arr as $data) { 
  isset($result[$data[&#39;key1&#39;]]) || $result[$data[&#39;key1&#39;]] = array(); 
  $result[$data[&#39;key1&#39;]][] = $data[&#39;key2&#39;]; 
}</span> 
ksort($cityAr, SORT_NATURAL);//这个是键值按字母先后顺序排列 
print_r($result); 
 
//输出如下 
Array 
( 
  [value1] => Array 
    ( 
      [0] => value2 
      [1] => value3 
    ) 
 
  [value2] => Array 
    ( 
      [0] => value4 
      [1] => value5 
    ) 
     
)

The above article in PHP allows members with the same value in the array to form a new array instance. This is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support php Chinese website

Articles you may be interested in:

Detailed explanation of solving the problem of inconsistent PHP string lengths

Executing php scripts from the command line $argv and $argc configuration method_php instance

thinkphp5 upload pictures and generate thumbnail public methods

The above is the detailed content of PHP lets the arrays with the same values ​​form a new array instance to explain. For more information, please follow other related articles on the PHP Chinese website!

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