从每行中删除第一个元素并将其值用作新的一级键

发布于 2024-12-08 00:50:03 字数 765 浏览 0 评论 0原文

有没有一种干净的方法来排列这个数组,使键变成 id ?我已经使用几个 foreach 和一个新数组来放置信息,但我使用了大约 7 行代码,想知道是否有更干净的东西。有问题的数组是:

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

那么我想从上面的数组中获取的是:

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

你可以看到我现在把 log id 作为键。这需要我写几行代码...您知道如何最多在 3 或 4 行内实现这一点吗?

或者,是否有更干净的方法来访问包含我想要的 *log_id* 的行?我想提供的用途是我可以访问某个 *log_id* 的行...在这种情况下,我将使用 $array[$log_id] 来实现...但是如果有一个解决方案可以在不改变数组(并且只有 1 行)的情况下做到这一点,我也会接受这个答案!

谢谢!

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 there is a solution to do this without altering the array (and with only 1 line) I will accept that as an answer too!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫七 2024-12-15 00:50:04

您不应该担心代码行,优雅的代码是自文档化的,这意味着您可以阅读它并理解它的作用。它包含所需的最少行数,这通常需要更长的编码时间。

与优雅代码相反的是压缩的、神秘的代码,就像这个代码,它可以满足您所讨论的数组的需要(我将其放入 $dataDemo):

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

就一行代码,但不够优雅。

这可以更好地阅读:

/**
 * 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;
}

大多数情况下,手头的数据以不需要转置的形式保存会更好。此外,如果您需要转置,请为自己创建一个可以完成这项工作的函数。

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.

猫瑾少女 2024-12-15 00:50:04

但为什么需要多个 foreach 语句呢?这不能回答您的问题吗:

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

唯一的区别是新行仍然具有 log_id 键,但我怀疑这会打扰您。除此之外,我发现它非常清楚。

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.

回忆那么伤 2024-12-15 00:50:04

我不确定是否有一种好方法可以做到这一点而不创建新数组(因为如果您只是对现有数组进行操作,则可能会发生关键冲突(在您的示例中,如果第一个项目的 log_id 为 1 会发生什么) ?)

可以使用 php 的 create_function 函数,但它的可读性非常差,无论使用 6 行还是 7 行都可能是最好的方法,

但为了论证,这里。是使用 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);
?>

出于好奇,您如何创建数组?如果它来自任何类型的数据库,那么在添加条目时创建密钥可能会更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文