1

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

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;
   }
4

3 回答 3

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']);

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

于 2013-02-05T16:44:27.180 回答
0

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

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

我没有机会对此进行测试,但它应该可以工作。

于 2013-02-05T16:37:04.973 回答
0
function doTheJob($inputArray, $lookingFor) {

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

或者,您可以使用array_filter而不是 external foreach

于 2013-02-05T16:44:12.780 回答