我当前的项目由多维数组组成,其中包含日期和一些文本内容。
我已经在我的项目中使用了普通数组,并且 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]
我怎样才能实现这一点,我是多维数组的初学者。
如有任何帮助,我们将不胜感激。
最佳答案
<?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
)
)
关于PHP:多维数组上的 array_push 并显示其元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248983/