PHP 数组重组

<?php

$a=array(0=>array('name'=>'aaaaa','id'=>'1'),1=>array('name'=>'aaaddaa','id'=>'1-1'),2=>array('name'=>'aaaaa','id'=>'1-2'),3=>array('name'=>'aaaaa','id'=>'2'),4=>array('name'=>'aaaaa','id'=>'2-1'));


var_dump($a);
$new=array();

foreach ($a as $key => $value) {
	if (!strpos($value['id'], '-')){
		$new[]=$value;
		unset($a[$key]);
	}
}

var_dump($new);

var_dump($a);

$new=array_merge($new,$a);

var_dump($new);


结果:
array(5) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "aaaddaa"
    ["id"]=>
    string(3) "1-1"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "1-2"
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "2"
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "2-1"
  }
}
array(2) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "2"
  }
}
array(3) {
  [1]=>
  array(2) {
    ["name"]=>
    string(7) "aaaddaa"
    ["id"]=>
    string(3) "1-1"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "1-2"
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "2-1"
  }
}
array(5) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(1) "2"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(7) "aaaddaa"
    ["id"]=>
    string(3) "1-1"
  }
  [3]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "1-2"
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(5) "aaaaa"
    ["id"]=>
    string(3) "2-1"
  }
}



			

发表评论

电子邮件地址不会被公开。 必填项已用*标注