Home >Backend Development >PHP Tutorial >php一维数组重新排序

php一维数组重新排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:04:291760browse

 Array ('id' => 3, 'username' => 'alex ','parentname'=> 'john'  
       ,'proname' => 'tttt' 'num'=> 1 ,  'userid' =>6 ,'parentid'=>8 ,
   'cpid'=>  0 ,'mobile'=> 15533336666 ,'parentmobile' => 156477765465) ;
 把最后两个分别放在第2个和第4个位置 ,怎么重新排序?
 Array ('id' => 3, 'username' => 'alex ','mobile'=> 15533336666 
      ,'parentname'=> 'john' ,'parentmobile' => 156477765465   ,'proname' => 'tttt' 'num'=> 1 , 
  'userid' =>6 ,'parentid'=>8 ,'cpid'=>  0  );


回复讨论(解决方案)

$a = Array(  'id' => 3,  'username' => 'alex ',  'parentname'=> 'john',  'proname' => 'tttt',  'num'=> 1 ,  'userid' =>6 ,  'parentid'=>8 ,  'cpid'=>  0 ,  'mobile'=> 15533336666 ,  'parentmobile' => 156477765465);$k = array (  'id',  'username',  'mobile',  'parentname',  'parentmobile',  'proname',  'num',  'userid',  'parentid',  'cpid',  'mobile',);extract($a);$b = compact($k);var_export($b);
array (  'id' => 3,  'username' => 'alex ',  'mobile' => 15533336666,  'parentname' => 'john',  'parentmobile' => 156477765465,  'proname' => 'tttt',  'num' => 1,  'userid' => 6,  'parentid' => 8,  'cpid' => 0,)

多谢了,能否不用extract和compact ,只用foreach能实现吗?谢谢了

如果没有样本,就不可能完成
如果有样本,还拘泥于形式,有什么意义吗?

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
Previous article:适配器模式和php实现Next article:桥接模式和php实现