Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按键值检索数组的子数组_Php_Arrays - Fatal编程技术网

Php 按键值检索数组的子数组

Php 按键值检索数组的子数组,php,arrays,Php,Arrays,我有以下数组(例如,真实数组更大) 现在,我想以某种方式“访问”具有给定id的子阵列,例如,访问具有id 984ab6aebd2777ff914e3e0170699c11的子阵列,然后继续在这样的foreach中使用此阵列 foreach ($array_with_specific_id as $event) { echo $event['message']; } 这可能吗 编辑: 在我的模型中生成数组的DB代码: public function get_event_timelin

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

现在,我想以某种方式“访问”具有给定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;
   }

我可能会去掉包含数组,因为它似乎不必要。但是,您可以执行以下操作:

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

我还没有机会测试它,但它应该可以工作。

我可能会去掉包含的数组,因为它似乎不必要。但是,您可以执行以下操作:

function get_message($specific_id, $arrays) {
    foreach($arrays as $array) {
        if(in_array($specific_id, $array)) {
            return $array['message'];
        }
    }
}
我还没有机会测试这个,但它应该可以工作

或者,您可以使用
array\u filter
而不是outer
foreach


或者,当从数据库填充数组时,您可以使用
array\u filter
而不是outer
foreach

从数据库填充数组时,您可以像下面的方案一样创建一个额外的
$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];
您可以通过调用

$number = count($index['984ab6aebd2777ff914e3e0170699c11']);

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

从数据库填充数组时,可以像下面的方案一样附加一个
$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];
您可以通过调用

$number = count($index['984ab6aebd2777ff914e3e0170699c11']);

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

它需要使用主数组的两个forach,对于该示例,是的,您可以。如何填充数组?从数据库?是的,我将添加DB代码您可以尝试它需要使用主数组的两个forach,对于该示例,是的,您可以。如何填充数组填充?从数据库?是的,我将添加DB代码你可以尝试什么是去除包含数组的最佳方法?查看我的帖子以查看模型代码什么是去除包含数组的最佳方法?查看我的帖子以查看模型代码这似乎很有效,谢谢..我还有一个小问题。查询可能会产生mult每个航班id的iple结果。现在只有一个有效。知道如何修复吗?我应该循环每个IDI的多个事件。我不确定您是否理解正确。在这种情况下,您希望我的代码:
$entry=$index[$flight\u id]
将同时返回两条记录,是吗?是的,没错!但可能不止两条记录Brilliant!现在有一些小改动可以很好地工作。非常感谢!这似乎可以工作了。不过,我还有一个小问题。该查询可能会对每个航班id产生多个结果。现在只有一个可以工作。知道如何解决吗?我应该oop通过每个IDI的多个事件,我不确定您是否理解正确。在这种情况下,您希望我的代码:
$entry=$index[$flight\u id]
将返回两条记录,是吗?是的,完全正确!但可能会有两条以上的记录!现在有一些小的更改可以正常工作。非常感谢!