3

How do I make this:

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

into this most efficiently?

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

5 Answers 5

6

Use array_map.

$a = array_map(function($e) { return $e['bleh']; }, $a);
Sign up to request clarification or add additional context in comments.

9 Comments

Good answer! Uses lambda expression which will limit it to PHP 5.3 only however.
Looks great, but not so efficient.
@danielsmith: w/o lambda: stackoverflow.com/questions/6614562/…
@Radu, by what benchmark? Perhaps something like $l = count($a); for ($i=0; $i<$l; ++$i) $a[$i] = $a[$i]['bleh']; is faster and uses less memory, but is the difference really going to be meaningful? One should generally focus on choosing the proper algorithm (every answer here is essentially the same) and write understandable code, and not worry so much about trivial optimization.
@konforce, I agree about writing readable code. All the answers are readable (arguably the foreach example is the most readable because you can understand what it does the second you look at it, without need for documentation). But as far as execution speed goes, this is twice as slow as the foreach example and the reset example is twice as slow as this one. If you have many values, you will see the difference.
|
4

try reset

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

Comments

3

If you need a fast solution (good execution speed) then:

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

Comments

2

I can not follow the measurements given in the comments below konforces answer, however this one is minimally faster than a foreach using refs:

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

Won't say it's trivial to actually measure it, the timings change a bit depending on which one comes first, this is over an array with ten million members as per the question:

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

Script:

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

Comments

0
<?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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.