1

I have an array of db records that I want to convert from this:

array(2) {
  [0]=>
  array(1) {
    ["ID"]=>
    string(1) "2"
  }
  [1]=>
  array(1) {
    ["ID"]=>
    string(1) "3"
  }
}

To this:

array(2) {
  [0]=>
    string(1) "2"
  [1]=>
    string(1) "3"
}

I'm looking for the fastest performing/easiest solution to this.

I couldn't find any PHP functions for this.

kba
  • 19,333
  • 5
  • 62
  • 89
Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37

5 Answers5

4

On PHP 5.5 or later, the simplest solution would be using PHP's built-in array_column() function.

$ids = array_column($arr, 'ID');
kba
  • 19,333
  • 5
  • 62
  • 89
3
$bas = array();
foreach ($foo as $bar) {
    $bas[] = $bar['ID'];
}

print_r($bas);

Where $foo would be you original array and $bas the one you want to convert it to.

federico-t
  • 12,014
  • 19
  • 67
  • 111
3

I wrote simple test for finding best solution between those:

// foreach
foreach ($array as $element) { $result[] = $element['ID']; }
// array_map
array_map(function($element) { return $element['ID']; }, $array);
// array_push
foreach ($array as $element) { array_push($result, $element['ID']); }

Results:

Test name   Repeats         Result          Performance    
foreach     1000            0,009204 sec    +0% 
array_push  1000            0,015731 sec    -70,915%
array_map   1000            0,024891 sec    -170,437%

From time to time they are slightly different, but foreach is always the best with outstanding performance results. So, seems @campari answer must be best answer. But i did not tested @kba solution, cause i'm on PHP 5.4. I suspect that array_column algorithm will show superior results.

Test code here: link.

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • Thanks. I suspected so. See these performance figures for JavaScript's `map` [here](http://stackoverflow.com/questions/10293378/what-is-the-most-efficient-way-of-merging-1-2-and-7-8-into-1-7-2-8/17910641#17910641). Though, if you can afford it, `array_map` is definitely a powerful and convenient choice, that can make use of its own stack-allocation - of course managing that "extravagance" is a crux when it comes to speed. – Lorenz Lo Sauer Aug 23 '13 at 20:33
2

Your input data (enriched with other data-types, -for testing-) and assuming you are using recent PHP versions:

$input = array(
    0=>
    array(
        "ID"=>  "2"
    ),
    1=>
    array(
        "ID"=>  "3"
    ),
    "iamnotanarray", 100, null
);

exemplary:

$out = array_map( function($el){ return @current($el);}, $input);

generally:

$out = array_combine(
    array_keys($ret)
    ,array_map( function($el){ return @current($el);}, $ret)
)

output:

var_export($out);

array (
    0 => '2',
    1 => '3',
    2 => NULL,
    3 => NULL,
    4 => NULL,
)

var_dump($out);

array(5) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "3"
  [2]=>
  NULL
  [3]=>
  NULL
  [4]=>
  NULL
}

To filter potentially unwanted data types, you may use:

$out = array_filter($out, is_string);
var_dump($out);
array(2) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "3"
}

I didn't time it (yet), but this is using PHP's native precompiled functions. Speed varies with the gcc compiler optimizations of your PHP executable.

Note: @current is dirty, not recommended and just used for brevity/readability. It would yield the same effect as is_array($el) ? current($el) : NULL;

Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
1

Try this-

$newArr = array();
foreach($array as $a)  // $array is original array 
{
    array_push($newArr, $a["ID"]);
}
print_r($newArr);
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • 2
    Hint: PHP has a function for that already called [`array_column`](http://php.net/array_column). In case you don't have PHP 5.5, there is a userland equivalent: http://benramsey.com/blog/2013/07/the-array-column-php-userland-library/ – hakre Aug 22 '13 at 21:59