嵌套数组,获取具有相同键的项目 [英] Nested array, get items with same key

查看:39
本文介绍了嵌套数组,获取具有相同键的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于嵌套数组的棘手问题.我从我的数据库中得到类似的信息:

I have a small tricky question with nested arrays. I am getting something like that from my database:

array
  0 => 
    array
      'id' => string '81' (length=2)
      'value' => string 'foobar' (length=6)
      'created_at' => string '2012-02-18 22:09:57' (length=19)
      'updated_at' => string '2012-02-18 22:09:57' (length=19)
  1 => 
    array
      'id' => string '106' (length=3)
      'value' => string 'barfoo' (length=6)
      'created_at' => string '2012-02-19 15:11:47' (length=19)
      'updated_at' => string '2012-02-19 15:11:48' (length=19)

我现在想要实现的是提取一个简单的关联数组,其中一个列"成为键,一个列"成为值.对于 case id/value,结果应该如下所示:

What I want to achieve now is to extract a simple associative array, where one "column" becomes the key and one "column" becomes the value. For the case id / value, the result should then look like that:

array
  81 => 'foobar'
  106 => 'barfoo'

我知道我可以执行嵌套循环来遍历所有数组,但我想知道是否有更快、更本地化的方法.我在玩 array_intersect,但它似乎没有提供我需要的东西.

I know that I could do nested loops to foreach through all of the arrays, but I was wondering if there is a quicker and more native method. I was playing around with array_intersect, but it does not seem to deliver what I need.

推荐答案

嗯,这个不涉及嵌套循环:

Well, this one doesn't involve nested loops:

$result = array();

foreach($queryResult as $row) {
    $result[$row['id']] = $row['value'];
}

这篇关于嵌套数组,获取具有相同键的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆