1

I have an array

$users    = array();

$users[0]['id']=1;
$users[0]['name']="user1";

$users[1]['id']=2;
$users[1]['name']="user2";

$users[2]['id']=3;
$users[2]['name']="user3";

And i need to extract id's into another array $ids; such that

$ids[0]=1;
$ids[1]=2;
$ids[2]=3;

I know one way to do it...

$ids    = array();
foreach($users as $user){
   $ids[]  = $user['id'];
}

But

1.is it is the best way to doit? 2.is it possible to do it without loops. 3.is it is the fastest way....?

7
  • 2
    I don't think you can do this w/out loops. Your code looks just fine. Commented Aug 6, 2012 at 8:46
  • It is possible to do it with array_walk()/array_map() etc - but this is just a loop in a different coat and in this case I suspect it would be less efficient than your simple, effective and easy to read foreach. So in short 1.is it is the best way to doit? - probably, yes. 2.is it possible to do it without loops. - definitely not, no. Even things that don't look like loops are just loops underneath (in the C++ source). 3.is it is the fastest way....? - again, probably yes. I'd be surprised if it wasn't. But with something that will take <10usec, speed is not really a concern. Commented Aug 6, 2012 at 8:54
  • 1. No, 2. No, 3. Probably, Seriously, even if there is fastest way to do it I doubt it will make any difference in your overall code. Commented Aug 6, 2012 at 8:57
  • @IvanHušnjak 1. No - care to expand? Or did you mean Yes? Commented Aug 6, 2012 at 8:58
  • @DaveRandom Oh sorry, you are right. I meant "yes, that is the best and simplest way to do it" Commented Aug 6, 2012 at 9:01

2 Answers 2

2

All the comments above address the question very well, but since noone has actually posted any actual answer, here's some additional info (to justify my answering it):

  • Is it the best way to do it?

Probably, but it is certainly the cleaner and most readable way to do it

  • is it possible to do it without loops?

Yes, but as people have said, it's only a trick, as loops will be used in the background.

  • is it is the fastest way?

Now this calls for some investigation. I have recreated a similar array as yours, using 100000 entries:

for ($i=0;$i<100000;$i++) {
    $users[] = array('id' => rand(),
            'name' => 'default');
}

And ran a few tests using different cases:

1.Plain old for loop (the one you have used yourself):

$ids=array();
$t = microtime(true);
foreach ($users as $key => $value) {
    $ids[] = $value['id'];
} 
echo microtime(true)-$t;

This required 0.085'' on average

2.Using array_walk():

$t = microtime(true);
array_walk($users, create_function('$v,$k,&$ids', '$ids[0][] = $v["id"];'), array(&$ids));
echo microtime(true)-$t;

This required on average 0.22'' (the same when using $GLOBALS['ids'] instead of this "reference" hack)

3.Using splFixedArray: This iterator is supposed to be faster than plain arrays. Indeed, the code above requires 0.075'' on average:

$users = new SplFixedArray(100000);
for ($i=0;$i<100000;$i++) {
    $users[$i] = array('id' => rand(),
            'name' => 'default');
}
$ids=array();
$t = microtime(true);
foreach ($users as $key => $value) {
    $ids[$key] = $value['id'];
}
echo microtime(true)-$t;

While the code below, where we use splFixedArray for both arrays, performed even faster, around 0.062'':

$users = new SplFixedArray(100000);
for ($i=0;$i<100000;$i++) {
    $users[$i] = array('id' => rand(),
            'name' => 'default');
}
$ids=new SplFixedArray(100000);
$t = microtime(true);
foreach ($users as $key => $value) {
    $ids[$key] = $value['id'];
}
echo microtime(true)-$t;

So, the bottom line is that no, it's not the fastest way, but it's the best if you take into account all 3 parameters you posed in your initial question

Sign up to request clarification or add additional context in comments.

4 Comments

+1: well said, although array_map is probably more appropriate than array_walk here. Shouldn't alter the performance numbers much though.
indeed, using array_map() is slightly better: it requires about 0.20'' on average
ok that's the answer;thanks to introducing me to SplFixedArray
Now in PHP 5.5 array_column does the job. php.net/manual/en/function.array-column.php
0
  • I think the best way is your way as programmer (but if you in team then the best way is the team way).
  • I think I'll go relying on built in PHP function (except some functions) for performance issues forever (cos I think the PHP peer work hard as needed to improve it).
  • I think it's possible to do it without loops.
$users = array();
$users[0]['id'] = 100;
$users[1]['id'] = 101;
$users[2]['id'] = 102;

$ids = array();
array_walk_recursive($users, function($val, $key) use(&$ids){
    if ($key == 'id') $ids[] = $val;
});

print_r($ids);


Array
(
    [0] => 100
    [1] => 101
    [2] => 102
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.