按键值检索数组的子数组

时间:2013-02-05 16:32:26

标签: php arrays

我有以下数组(例如,实数更大)

Array
(
    [0] => Array
        (
            [984ab6aebd2777ff914e3e0170699c11] => Array
                (
                  [id] => 984ab6aebd2777ff914e3e0170699c11
                  [message] => Test1

        )

    [1] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test2
                )

        )

    [2] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test3

        )

    [3] => Array
        (
            [ca403872d513404291e914f0cad140de] => Array
                (
                  [id] => ca403872d513404291e914f0cad140de
                  [message] => Test4
                )

        )
)

现在我想以某种方式“访问”具有给定id的子阵列,例如访问ID为984ab6aebd2777ff914e3e0170699c11的子阵列,然后继续在这样的foreach中使用这个数组..

foreach ($array_with_specific_id as $event) {
    echo $event['message'];
}

这可能吗?

编辑:

在我的模型中生成数组的DB代码:

  public function get_event_timeline($id)
   {
     $data = array();
      foreach ($id as $result) {
          $query = $this->db->query("SELECT * FROM event_timeline WHERE id = ?", array($result['id']));
          foreach ($query->result_array() as $row)
          {
               array_push($data, array($row['id'] => $row));
          }
      }

      return $data;
   }

3 个答案:

答案 0 :(得分:1)

从数据库填充数组时,您可以像以下方案一样处理额外的$index数组:

$index = array (
    '984ab6aebd2777ff914e3e0170699c11' => ReferenceToElementInData,
    'ca403872d513404291e914f0cad140de' => ReferenceToElementInData,
    // ...
)

这可以让您通过他们的id快速访问元素,而无需额外的foreach循环。当然它需要额外的内存,但这应该没问题,因为你只会保存对原始数据的引用。但是,测试一下。

这是一个例子:

public function get_event_timeline($id)
{
  $data = array();

  // create an additional index array
  $index = array();

  // counter 
  $c=0;

  foreach ($id as $result) {
      $query = $this->db->query("SELECT * FROM event_timeline WHERE id = ?", array($result['id']));

      // every entry in the index is an array with references to entries in $data
      $index[$result['id']] = array();

      foreach ($query->result_array() as $row)
      {
           array_push($data, array($row['id'] => $row));
           // create an entry in the current index
           $index[$row['id']][] = &$data[$c];
           $c++;
      }
  }

  return array (
      'data' => $data,
      'index' => $index
  );
}

现在,您可以通过索引数组访问元素,而无需额外的foreach循环:

$entry = $index['984ab6aebd2777ff914e3e0170699c11'][0];

如果每个$id有多个结果(正如您在评论中提到的那样,您可以使用大于零的索引访问它们:

$entry = $index['984ab6aebd2777ff914e3e0170699c11'][1];
$entry = $index['984ab6aebd2777ff914e3e0170699c11'][2];

您可以通过调用

获取每$id项的计数
$number = count($index['984ab6aebd2777ff914e3e0170699c11']);

这与在数据库中用于加速查询的索引非常相似。

答案 1 :(得分:0)

我可能只是摆脱了包含数组,因为它似乎没必要。但是,您可以执行以下操作:

function get_message($specific_id, $arrays) {
    foreach($arrays as $array) {
        if(in_array($specific_id, $array)) {
            return $array['message'];
        }
    }
}

我没有机会对此进行测试,但工作。

答案 2 :(得分:0)

function doTheJob($inputArray, $lookingFor) {

    foreach ($inputArray as $subArray) {
        foreach ($subArray as $subKey => $innerArray) {
            if ($subKey == $lookingFor) {
                return $innerArray;
            }
        }
    }
    return NULL;
}

或者,您可以使用array_filter代替外foreach

相关问题