如果我有以下数组:
$array = array(
"01" => "0",
"02" => "0",
"03" => "0",
"04" => "0",
"05" => "0",
"06" => "0",
"07" => "0",
"08" => "0",
"09" => "0",
"10" => "0",
"11" => "0",
"12" => "0"
);
我怎么能命令它像:
$array = array(
"05" => "0",
"06" => "0",
"07" => "0",
"08" => "0",
"09" => "0",
"10" => "0",
"11" => "0",
"12" => "0",
"01" => "0",
"02" => "0",
"03" => "0",
"04" => "0"
);
甚至更好的方法是如何从特定键开始回显每个值并在需要时返回?
修改
我最终以不同的方式工作。
$first_value = 05;
$where_to_slice = $first_value + 0; // this is just to remove any leading 0 because the $first_value was being retrieved from a DB as 05.
$array_owner_data_1 =(array_slice($array_owner_data, $where_to_slice));
$array_owner_data_2 =(array_slice($array_owner_data, 0,$where_to_slice));
echo implode(", ", $array_owner_data_1);
echo ",";
echo implode(", ", $array_owner_data_2);
所以现在如果我连续打印两个切片,我会按照自己的意愿订购数据。
答案 0 :(得分:1)
这里有一个旋转功能,你不能使用array_shift,键会被改变。
<?php
$array = array(
"01" => "0",
"02" => "0",
"03" => "0",
"04" => "0",
"05" => "0",
"06" => "0",
"07" => "0",
"08" => "0",
"09" => "0",
"10" => "0",
"11" => "0",
"12" => "0"
);
var_dump($array);
function rotate(&$a) {
reset($a);
$value = current( $a );
$key = key( $a );
unset( $a[ $key ]);
$a[$key] = $value;
}
for ($i = 0; $i < 4; $i++) {
rotate($array);
}
var_dump($array);
?>
输出:
array(12) {
["01"]=>
string(1) "0"
["02"]=>
string(1) "0"
["03"]=>
string(1) "0"
["04"]=>
string(1) "0"
["05"]=>
string(1) "0"
["06"]=>
string(1) "0"
["07"]=>
string(1) "0"
["08"]=>
string(1) "0"
["09"]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(1) "0"
[12]=>
string(1) "0"
}
array(12) {
["05"]=>
string(1) "0"
["06"]=>
string(1) "0"
["07"]=>
string(1) "0"
["08"]=>
string(1) "0"
["09"]=>
string(1) "0"
[10]=>
string(1) "0"
[11]=>
string(1) "0"
[12]=>
string(1) "0"
["01"]=>
string(1) "0"
["02"]=>
string(1) "0"
["03"]=>
string(1) "0"
["04"]=>
string(1) "0"
}
答案 1 :(得分:1)
设置您要开始的索引:
$array = array(
'01' => '0',
'02' => '0',
'03' => '0',
'04' => '0',
'05' => '0',
'06' => '0',
'07' => '0',
'08' => '0',
'09' => '0',
'10' => '10',
'11' => '11',
'12' => '12'
);
function beginAt($array,$index){
$arr1=$arr2=$resp=array();
foreach($array as $k=>$v){if((int)$k < $index){$arr1[(string)$k] = $v;}else{$arr2[(string)$k] = $v;}}
foreach($arr2 as $k=>$v)$resp[(string)$k]=$v;
foreach($arr1 as $k=>$v)$resp[(string)$k]=$v;
return $resp;
}
var_dump(beginAt($array,7));
答案 2 :(得分:1)
这应该适合你:
$month = "05";
$array = array(
"01" => "0",
"02" => "0",
"03" => "0",
"04" => "0",
"05" => "0",
"06" => "0",
"07" => "0",
"08" => "0",
"09" => "0",
"10" => "0",
"11" => "0",
"12" => "0"
);
$newArray = array();
for($i = $month; $i <= count($array)+$month; $i++)
$newArray[($i > 12 ? sprintf("%02d", $i-12) : sprintf("%02d", $i))] = $array[($i > 12 ? sprintf("%02d", $i-12) : sprintf("%02d", $i))];
print_r($newArray);
答案 3 :(得分:0)
试试这段代码
$ordering_position = 5;
$new_array = array();
$array_pos = 0
function order_array($ordering_position,$array){
while ($ordering_position < strlen($array){
$new_array[$array_pos] = $array[$ordering_position];
$array_pos = $array_pos + 1
$ordering_position = $ordering_position + 1
if ($array_pos == strlen($array)){
order_array(0,$array);
break;
}
}
}
echo new_array;