开发者

Re-arranging an array

开发者 https://www.devze.com 2023-04-10 18:07 出处:网络
Is there a clean way to arrange this array so that the key becomes id? I have done this several times using a couple foreach and a new array where I put the info, but I am using around 7 lines of code

Is there a clean way to arrange this array so that the key becomes id? I have done this several times using a couple foreach and a new array where I put the info, but I am using around 7 lines of code and was wondering if there is anything cleaner. The array in question is:

Array ( 
  [0] => Array ( [log_id] => 6  [type] => Test   [other_info] => MoreInfo ) 
  [1] => Array ( [log_id] => 5  [type] => Test2  [other_info] => MoreInfo2 ) 
)

So what I want to obtain from the above array is:

Array ( 
  [6] => Array ( [type] => Test   [other_info] => MoreInfo ) 
  [5] => Array ( [type] => Test2  [other_info] => MoreInfo2 ) 
)

You can see that I put log id as the key now. This is taking me several lines of code... Do you have an idea of how this can be achieved in 3 or 4 lines at most?

Alternatively, is there a cleaner way to access the row containing the *log_id* that I want? The use that I want to give to this is that I can access the row of a certain *log_id*... In this case I would do it with $array[$log_id]... But if t开发者_如何学Pythonhere is a solution to do this without altering the array (and with only 1 line) I will accept that as an answer too!

Thanks!


You should not be concerned about lines of code that, elegant code is self-documenting, meaning you can read it and understand what it does. And it contains the bare minimum of lines it needs, which generally takes longer to code.

The opposite to elegant code is compressed, cryptic code, like this one which does what you need for the array in question (I put it into $data, Demo):

$data = array_reduce($data, function($v, $w) { $v[array_shift($w)] = $w; return $v;});

It's just one line of code, but it's not elegant.

This could be read much better:

/**
 * You can leave a comment here.
 * @return array $data keyed with first value of each value
 */
function first_as_key(Array $data)
{
    $keyed = array();
    foreach($data as $v)
        $keyed[array_shift($v)] = $v
        ;
    return $keyed;
}

Most often it's much better to have the data at hand in a form that does not need to transpose it. Additionally if you need to transpose, create yourself a function that does the job for you.


But why do you need multiple foreach statements? Wouldn't this answer your question:

$new_arr = array();
foreach ($old_arr as $row) {
    $new_arr[$row['log_id']][] = $row;
}

The only difference is that the new rows will still have the log_id key, but I doubt that's going to bother you. Except that, I find it very clear.


I'm not sure there's a good way to do this and not create a new array (since if you just operated on the existing array you could have key collisions (in your example, what would happen if the first item had a log_id of 1?)

It's possible to put this into a single line using php's create_function function, but it's pretty bad for readability. Whatever you have using 6 or 7 lines is probably the best way to do it.

But for argument's sake, here is one way to do it in 2 lines using array_walk:

<?php 
$test = array();
$test[] = array('id' => 6, 'data' => 'asdf');
$test[] = array('id' => 7, 'data' => 'qwer');

print_r($test);

$result = array();
array_walk($test, create_function('&$val,$key', 'global $result; $result[$val[\'id\']] = $val;'));
print_r($result);
?>

Out of curiosity, how are you creating the array? If it's coming from any sort of database, it is probably better to create the key when you add the entry.

0

精彩评论

暂无评论...
验证码 换一张
取 消