【问题标题】:PHP: array_push on multidimensional arrays and display its elementsPHP:array_push 多维数组并显示其元素
【发布时间】:2015-01-31 06:39:12
【问题描述】:

我当前的项目由多维数组组成,其中包含日期和一些文本内容。

我已经在我的项目中使用了普通数组,并且 array_push 用于将元素插入到数组中。不,我被困在多维数组中,我不知道如何插入和显示多维数组(多维数组的初学者)。

我从 stackoverflow 本身发现了很多结果,但没有一个对我有帮助。我创建了一个这样的多维数组

$complaints = array(
    $each_complaints => array(
            "date" => "",
            "text" => ""
            )
 );

然后我想在mysql结果的循环中将数据添加到这个数组中

<?php foreach($query_56 as $notes):
          // eg: array_push " $notes->date , $notes->corresponding_text "
endforeach; ?>

我想像这样显示数组

array[date][text]=> [2014-11-18] [1st complaint]
array[date][text]=> [2015-01-15] [2nd complaint]

我怎样才能做到这一点,我是多维数组的初学者。

任何帮助将不胜感激。

【问题讨论】:

  • 你当前的数组结构打印如下 Array ( [] => Array ( [date] => [text] => ) ) 数组的索引为空

标签: php mysql arrays codeigniter multidimensional-array


【解决方案1】:
<?php foreach($query_56 as $key=>$notes):
       $each_complaints[$key]["date"] = $notes->date;
       $each_complaints[$key]["text"] = $notes->corresponding_text ;           
endforeach;

echo "<pre>";print_r($each_complaints);
?>
**Output :** 
(
[0] => Array
    (
        [date] => 01-11-2014
        [text] => rrrrrr
    )

[1] => Array
    (
        [date] => 02-11-2014
        [text] => fffff
    )

[2] => Array
    (
        [date] => 03-11-2014
        [text] => ddddd
    )

)

【讨论】:

  • 感谢 raj 的即时回复。目标已实现 :) 但在第 5 行也显示错误“未定义变量:each_complaints”,但第 5 行是数组声明
  • @ShifanaMubi :只需声明 $each_complaints = array();在 foreach 循环之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
相关资源
最近更新 更多