【问题标题】:Converting from Array of Arrays to Single Array从数组数组转换为单个数组
【发布时间】:2011-07-07 17:22:15
【问题描述】:

我该怎么做:

array(2) {
  [0] => array(1) {
    ["bleh"] => int(109720)
  }
  [1] => array(1) {
    ["bleh"] => int(112439)
  }
}

进入这个最有效?

array(2) {
  0 => 109720,
  1 => 112439
}

【问题讨论】:

    标签: php arrays key


    【解决方案1】:

    使用array_map

    $a = array_map(function($e) { return $e['bleh']; }, $a);
    

    【讨论】:

    • 好答案!使用 lambda 表达式,这会将其限制为仅 PHP 5.3。
    • 看起来不错,但效率不高。
    • @danielsmith: w/o lambda: stackoverflow.com/questions/6614562/…
    • @Radu,通过什么基准测试?也许像$l = count($a); for ($i=0; $i<$l; ++$i) $a[$i] = $a[$i]['bleh']; 这样的东西更快并且使用更少的内存,但是差异真的有意义吗?人们通常应该专注于选择合适的算法(这里的每个答案都基本相同)并编写易于理解的代码,而不是太担心琐碎的优化。
    • @konforce,我同意编写可读代码。所有答案都是可读的(可以说foreach 的例子是最易读的,因为你可以在看到它的第二秒就理解它的作用,而无需文档)。但就执行速度而言,这比foreach 示例慢两倍,reset 示例比这个慢两倍。如果你有很多值,你会看到差异。
    【解决方案2】:

    试试reset

      $result = array_map("reset", $a);
    

    【讨论】:

      【解决方案3】:

      如果您需要快速的解决方案(良好的执行速度),那么:

      $a = /* the original array */;
      $b = array();
      foreach ($a as $value) {
          $b[] = $value['bleh'];
      }
      

      【讨论】:

        【解决方案4】:

        我无法遵循 konforces 答案下面的 cmets 中给出的测量值,但是这个比使用 refs 的 foreach 快一点:

        $c=count($array);
        for(
          $i=0;
          $i<$c
          ;
          $array[$i]=$array[$i]['bleh'],
          $i++
        );
        

        不会说实际测量它是微不足道的,时间会有所不同,具体取决于哪个先出现,根据问题,这是一个拥有一千万成员的数组:

        foreach ref: 4.192161
        foreach key: 4.383342
        foreach copy: 4.222771
        array_map lambda: 12.240275
        array_map reset: 16.401093
        for key: 3.459406
        for copy: 4.690722
        

        脚本:

        ini_set('memory_limit', -1); // wer're going to consume a lot.
        $arrayCount = 10000000;
        
        $test = 'just run';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $array = end($array);
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $array = array_map(function(){}, $array);
        
        $test = 'foreach ref';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        foreach($array as &$v) $v = $v['bleh'];
        unset($v);
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        printf("%s: %f\n", $test, $diff);
        
        $test = 'foreach key';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        foreach($array as $k => $v) $array[$k] = $v['bleh'];
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        
        printf("%s: %f\n", $test, $diff);
        $test = 'foreach copy';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        foreach($array as $k => $v) $arrayb[] = $v['bleh'];
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        unset($arrayb);
        printf("%s: %f\n", $test, $diff);
        
        $test = 'array_map lambda';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        $array = array_map(function($e) { return $e['bleh']; }, $array);
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        printf("%s: %f\n", $test, $diff);
        
        $test = 'array_map reset';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        $array = array_map('reset', $array);
        foreach($array as $k => $v) $arrayb[] = $v['bleh'];
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        printf("%s: %f\n", $test, $diff);
        
        $test = 'for key';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        for($i=0,$c=count($array);$i<$c;$array[$i]=$array[$i]['bleh'],++$i);
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        printf("%s: %f\n", $test, $diff);
        
        $test = 'for copy';
        $array = array_fill(0, $arrayCount, array("bleh" => 109720,));
        $start = microtime(1);
        for($i=0,$c=count($array);$i<$c;$arrayb[]=$array[$i]['bleh'],++$i);
        $diff = microtime(1)-$start;
        $tests[$test] = $diff;
        unset($arrayb);
        printf("%s: %f\n", $test, $diff);
        

        【讨论】:

          【解决方案5】:
          <?php
          $all = array(
              array( "one"    => 1),
              array( "two"    => 2),
              array( "three"  => 3)
          );
          
          function getKey($item)
          {
              return key($item);
          }
          
          function getVal($item)
          {
              return $item[key($item)];
          }
          $keys   = array_map("getKey", $all);
          $values = array_map("getVal", $all);
          print_r($keys);
          print_r($values);
          
          OUTPUT:
          
          Array (
              [0] => one
              [1] => two
              [2] => three ) 
          Array (
              [0] => 1
              [1] => 2
              [2] => 3 )
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-11-28
            • 2011-10-10
            • 1970-01-01
            • 1970-01-01
            • 2014-08-31
            • 1970-01-01
            相关资源
            最近更新 更多