代码之家  ›  专栏  ›  技术社区  ›  Mohd Abdul Mujib

从特定数字重新索引数组

  •  2
  • Mohd Abdul Mujib  · 技术社区  · 7 年前

    如何重新索引从特定数字开始而不是从0开始的数组。

    我知道只需循环遍历数组并创建一个新的数组 Key => Value 键是自定义数字,然后在每次迭代中递增。

    $custom_index = 5;
    $output = [];
    foreach($input as $val){
     $output[$custom_index++] = $val;
    }
    

    还有其他的吗(也许更好?)如何在php中用自定义start设置数组的索引?

    3 回复  |  直到 7 年前
        1
  •  4
  •   William J.    7 年前

    也许不是更好,但这里有另一种方法:

    $customIndex = 5;
    $output = [];
    // example input array
    $input = [1,2,3,4,5];
    
    $indexes = range($customIndex, $customIndex + count($input) - 1);
    $output = array_combine($indexes, $input);
    
    var_dump($output);
    
    // prints out: 
    array(5) {
    [5]=>
    int(1)
    [6]=>
    int(2)
    [7]=>
    int(3)
    [8]=>
    int(4)
    [9]=>
    int(5)
    }
    
        2
  •  1
  •   Nico    7 年前

    另一种选择(可能不是最好的)可能是这样的:

    $array = array("five","six","seven");
    $result = array_flip(array_map(function($n){
       return($n+5); // custom index
    }, array_flip($array)));
    
    print_r($result);
    

    输出为:

    Array ( [5] => five [6] => six [7] => seven ) 
    

    我们可以利用 array_flip 结合 array_map ,首先我们切换索引和值,所以 阵列图 可以增加数组的值,然后切换回值和索引。

    编辑 :

    这是我的代码、你的代码和@William Janoti的性能的粗略比较

    $time_start = microtime(true);
    $array = array_fill (0,100000,"test");
    $result = array_flip(array_map(function($n){ return($n+5);}, array_flip($array)));     
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "Process Time 1: {$time}";
    
    $time_start = microtime(true);
    $input =  array_fill (0,100000,"test");
    $custom_index = 5;
    $output = [];
    foreach($input as $val){
     $output[$custom_index++] = $val;
    }
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "Process Time 2: {$time}";
    
    
    
    $time_start = microtime(true);
    $customIndex = 5;
    $output = [];
    // example input array
    $input = array_fill (0,100000,"test");
    $indexes = range($customIndex, $customIndex + count($input) - 1);
    $output = array_combine($indexes, $input);
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "Process Time 3: {$time}";
    

    结果:

    Process Time 1: 0.012617826461792  
    Process Time 2: 0.026544094085693  
    Process Time 3: 0.028899908065796
    
        3
  •  0
  •   Debuqer    7 年前

    如果你不需要实际的重新索引,你也可以使用函数来实现Reindex。

    function get_array_index(&$array, $index, $index_start=0)
    {
        return $array[$index_start - $index] ;
    }
    

    您可能应该创建新函数来使用数组。

    我写了两个函数,这样就可以重新索引伪数组。

    function array_get(&$array, $index, $index_start=0)
    {
        return $array[$index - $index_start] ;
    }
    function array_set(&$array, $index, $val, $index_start=0)
    {
        $array[$index - $index_start] = $val;
    }
    
    $index_start = 0 ;  // what is the first offset
    
    // input 
    $input = [1,2,3,4,5] ;
    
    // get index 0
    echo $input[0] ; // 1 ; this line refer is actuall array
    echo '<br/>';
    echo array_get($input, 0, $index_start) ; // 1 ; this is what we goona use for re-indexing purpose
    echo '<br/>';
    
    // get index 2
    echo $input[2] ; // 3 
    echo '<br/>';
    echo array_get($input, 2, $index_start) ; // 3
    echo '<br/>';
    
    // reset $input[2] 
    array_set($input, 2, 12, $index_start) ; // change 3 -> 12
    echo array_get($input, 2, $index_start) ; // 12
    echo '<br/>';
    
    
    // let's re-index array 
    $index_start = 5 ;  // first index is 5 now
    
    
    // it's seems to a re-index happend but the actuall array did'nt changed
    
    echo array_get($input, 5, $index_start) ; // must be 1
    echo '<br/>';
    echo array_get($input, 6, $index_start) ; // must be 2
    echo '<br/>';
    echo array_get($input, 7, $index_start) ; // must be 12
    echo '<br/>';
    echo array_get($input, 8, $index_start) ; // must be 4
    echo '<br/>';
    echo array_get($input, 9, $index_start) ; // must be 5
    echo '<br/>';
    
    // reset $input[9] 
    array_set($input, 9, 15, $index_start) ; // change 5 -> 15
    echo array_get($input, 9, $index_start) ; // 15
    echo '<br/>';
    

    如果你真的需要重新索引数组,这个函数对你没有帮助。