重新排序数组或从特定位置打印

Re-order array or print from specific position

如果我有以下数组:

$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);

所以现在,如果我连续打印两个切片,我就会得到我想要的数据。

试试这个代码

$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;

这里是旋转功能,不能用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"
}

设置您要开始的索引:

$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));

这应该适合你:

$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);