Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The purpose of this code is to change each column to a row, and assign keys to each value for the new array according to the old array. I would like to know if there're other ways to optimize it.

<?php

$arr = [
    'name' => ['a', 'b', 'c'],
    'age'  => [ 2 ,  1 ,  3 ]
];

$result = [];
$keys   = array_keys($arr);
foreach($arr[$keys[0]] as $index => $temp) {
    $data = [];
    foreach($keys as $i => $key) {
        $data[$key] = $arr[$key][$index];
    }
    $result[] = $data;
}

print_r($result);

Which gives:

$result = [
    ['name' => 'a', 'age' => 2],
    ['name' => 'b', 'age' => 1],
    ['name' => 'c', 'age' => 3],
];
share|improve this question

There isn't really any great performance improvement to be made here in my opinion. You original structure is going to require nested loop approach to generate your desired structure regardless.

My only suggestion would be building an array of objects in your result rather than an array of associative arrays, as I think what you have shown in your example is more meaningful as objects (i.e. items that have a set of properties) vs. as an associative array (which I typically like to use more for hashmap type of use cases).

share|improve this answer

You can use normal for loop like this :

<?php

  $arr = [
'name' => ['a', 'b', 'c'],
'age'  => [ 2 ,  1 ,  3 ]
 ];

 $new_array=array();
 $acount=count($arr['name']);

 for($i=0;$i<$acount;$i++){

 $new_array[$i]['name']=$arr['name'][$i];

 $new_array[$i]['age']=$arr['age'][$i];

 }


?>
share|improve this answer
1  
You certainly could do that. Why would you want to do so? In particular, note that the original code handled arbitrary array keys. You're manually specifying each key, so you'd have to rewrite the code for each task. So you should show some advantage to this code that outweighs the flexibility of the original code. In general, remember that we are Code Review. Why we make decisions in our code is as important as the code itself. – mdfst13 Oct 25 at 15:07

If you have 2 attributes: name, age and unique name, may be helpful I think)

$userData = array_combine($arr['name'], $arr['age']);

foreach($userData as $name => $age) { }
share|improve this answer

Like you, I don't see a better way than basing the method on evaluating array_keys($arr), then iterate it to build each new member.

But there are two possible slight improvement in how to it:

$arr = [
    'name' => ['a', 'b', 'c'],
    'age'  => [ 2 ,  1 ,  3 ]
];

$result = [];
$keys = array_keys($arr);
for ($row = 0,  $rows = count(reset($arr)); $row < $rows; $row++) {
  foreach ($keys as $key) {
    $result[$row][$key] = $arr[$key][$row];
  }
}

echo '<pre>' . print_r($result, true) . '</pre>';

The first (and somewhat obvious) improvement is: not to use intermediary variable data.

I'm not really sure of the second one: I tend to think that the for() loop will be faster, because it accesses $arr only once (count(reset($arr))), while the foreach() loop have to extract data from $arr on each step.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.