PHP更改数组键

Array( 0 => 'blabla', 1 => 'blabla', 2 => 'blblll' ) etc.. 

有没有办法改变所有的数字键“名”没有循环通过数组(所以一个PHP函数)?

如果你有一个你想使用的键数组,然后使用array_combine

给定$ keys = array('a','b','c',…)和数组$ list,然后执行以下操作:

 $list = array_combine($keys, array_values($list)); 

列performance在是数组('a'=>'blabla 1',…)等

您必须使用array_values从数组中提取值而不是旧的数字键。

这是很好,简单的看,但array_values使整个arrays的副本,所以你可能有空间问题。 我们在这里做的是让PHP为我们做循环,而不是消除循环。 我会试图做更多的事情:

 foreach ($list as $k => $v) { unset ($list[$k]); $new_key = *some logic here* $list[$new_key] = $v; } 

我不认为它比第一个代码更有效,但它提供了更多的控制,并且不会对数组长度产生影响。

不,对于初学者来说,不可能有一个元素共享同一个键的数组

 $x =array(); $x['foo'] = 'bar' ; $x['foo'] = 'baz' ; #replaces 'bar' 

其次,如果你只想在数字前加上这个数字

 $x[0] --> $x['foo_0'] 

这在没有循环的情况下在计算上是不可信的。 目前没有任何“密钥前缀”任务的PHP函数,最接近的是“提取” ,它将数字键作为前缀variables之前。

最简单的方法是:

 function rekey( $input , $prefix ) { $out = array(); foreach( $input as $i => $v ) { if ( is_numeric( $i ) ) { $out[$prefix . $i] = $v; continue; } $out[$i] = $v; } return $out; } 

此外,在阅读XMLWriter的使用,我相信你会写一个坏的方式XML。

 <section> <foo_0></foo_0> <foo_1></foo_1> <bar></bar> <foo_2></foo_2> </section> 

不好的XML。

 <section> <foo></foo> <foo></foo> <bar></bar> <foo></foo> </section> 

是更好的XML,因为当被解释时,被重复的名称并不重要,因为它们全都被数字地偏移,如下所示:

 section => { 0 => [ foo , {} ] 1 => [ foo , {} ] 2 => [ bar , {} ] 3 => [ foo , {} ] } 
 $prefix = '_'; $arr = array_combine( array_map(function($v) use ($prefix){ return $prefix.$v; }, array_keys($arr)), array_values($arr) ); 

我添加了这个答案来回答另一个问题,似乎相关。 希望可以帮助需要更改数组中键的值的人。 使用php的内置函数。

 $inputArray = array('app_test' => 'test', 'app_two' => 'two'); /** * Used to remap keys of an array by removing the prefix passed in * * Example: * $inputArray = array('app_test' => 'test', 'app_two' => 'two'); * $keys = array_keys($inputArray); * array_walk($keys, 'removePrefix', 'app_'); * $remappedArray = array_combine($keys, $inputArray); * * @param $value - key value to replace, should be from array_keys * @param $omit - unused, needed for prefix call * @param $prefix - prefix to string replace in keys */ function removePrefix(&$value, $omit, $prefix) { $value = str_replace($prefix, '', $value); } // first get all the keys to remap $keys = array_keys($inputArray); // perform internal iteration with prefix passed into walk function for dynamic replace of key array_walk($keys, 'removePrefix', 'app_'); // combine the rewritten keys and overwrite the originals $remappedArray = array_combine($keys, $inputArray); // see full output of comparison var_dump($inputArray); var_dump($remappedArray); 

输出:

 array(2) { 'attr_test' => string(4) "test" 'attr_two' => string(3) "two" } array(2) { 'test' => string(4) "test" 'two' => string(3) "two" } 

我认为他想要:

 $a = array(1=>'first_name', 2=>'last_name'); $a = array_flip($a); $a['first_name'] = 3; $a = array_flip($a); print_r($a); 

当您使用XMLWriter (本地到PHP 5.2.x <)使用$xml->startElement('itemName'); 这将取代数组键。

在php中使用数组array_flip

 $array = array ( [1] => Sell [2] => Buy [3] => Rent [4] => Jobs ) print_r(array_flip($array)); Array ( [Sell] => 1 [Buy] => 2 [Rent] => 3 [Jobs] => 4 ) 

我为一组对象做了这个。 它基本上是在同一个arrays中创build新的密钥,并取消旧的密钥。

 public function transform($key, $results) { foreach($results as $k=>$result) { if( property_exists($result, $key) ) { $results[$result->$key] = $result; unset($results[$k]); } } return $results; } 
 <?php $array[$new_key] = $array[$old_key]; unset($array[$old_key]); ?> 

将数组键名称“group”更改为“children”。

 <?php echo json_encode($data); function array_change_key_name( $orig, $new, &$array ) { foreach ( $array as $k => $v ) { $res[ $k === $orig ? $new : $k ] = ( (is_array($v)||is_object($v)) ? array_change_key_name( $orig, $new, $v ) : $v ); } return $res; } echo '<br>=====change "group" to "children"=====<br>'; $new = array_change_key_name("group" ,"children" , $data); echo json_encode($new); ?> 

结果:

 {"benchmark":[{"idText":"USGCB-Windows-7","title":"USGCB: Guidance for Securing Microsoft Windows 7 Systems for IT Professional","profile":[{"idText":"united_states_government_configuration_baseline_version_1.2.0.0","title":"United States Government Configuration Baseline 1.2.0.0","group":[{"idText":"security_components_overview","title":"Windows 7 Security Components Overview","group":[{"idText":"new_features","title":"New Features in Windows 7"}]},{"idText":"usgcb_security_settings","title":"USGCB Security Settings","group":[{"idText":"account_policies_group","title":"Account Policies group"}]}]}]}]} =====change "group" to "children"===== {"benchmark":[{"idText":"USGCB-Windows-7","title":"USGCB: Guidance for Securing Microsoft Windows 7 Systems for IT Professional","profile":[{"idText":"united_states_government_configuration_baseline_version_1.2.0.0","title":"United States Government Configuration Baseline 1.2.0.0","children":[{"idText":"security_components_overview","title":"Windows 7 Security Components Overview","children":[{"idText":"new_features","title":"New Features in Windows 7"}]},{"idText":"usgcb_security_settings","title":"USGCB Security Settings","children":[{"idText":"account_policies_group","title":"Account Policies group"}]}]}]}]} 

要有相同的键,我认为他们必须在单独的嵌套数组。

 for ($i = 0; $i < count($array); $i++) { $newArray[] = ['name' => $array[$i]]; }; 

输出:

 0 => array:1 ["name" => "blabla"] 1 => array:1 ["name" => "blabla"] 2 => array:1 ["name" => "blblll"] 

你可以创build一个包含该数组的新数组,所以:

 <?php $array = array(); $array['name'] = $oldArray; ?>