如何在数组中获取数据库中的记录值。我想加入当天的号码。
array
[0] => array=>'id'=>'26' 'date'=>'26'
[1] => array=>'id'=>'27' 'date'=>'27',
array=>'id'=>'28' 'date'=>'27',
array=>'id'=>'29' 'date'=>'27'
[2] => array=>'id'=>'30' 'date'=>'29'
[3] => array=>'id'=>'31' 'date'=>'31',
array=>'id'=>'32' 'date'=>'31',
array=>'id'=>'33' 'date'=>'31'
基本上,如果下一个id包含一个月份相同的日期(no no)的记录,我想在同一个索引中添加一个数组。否则正常添加。
现在,我的功能是添加记录的行而不按我希望的格式对其进行排序。
我希望它采用这种格式的原因是因为,我需要运行foreach,如果1天包含2条记录,那么它会将另一个<li>
附加到我的无序列表中。
public function getArticles()
{
$sql = 'CALL getArticles()';
$articles = Array();
if (Model::getConnection()->multi_query($sql)) {
do {
if ($result = Model::getConnection()->store_result()) {
while ($row = $result->fetch_assoc()) {
array_push($articles,$row);
}
$result->free();
}
} while (Model::getConnection()->next_result());
}
return $articles;
}
答案 0 :(得分:0)
我不知道你的一些代码在做什么,但我认为这是重要的部分。
while ($row = $result->fetch_assoc()) {
if (!isset($articles[$row['date']])) {
$articles[$row['date']] = array();
}
$articles[$row['date']][] = $row;
}
唯一的区别是你的数组将被锁定在date
而不是从零递增。如果你真的想要重新编制索引,你可以做...
array_values($articles);
答案 1 :(得分:0)
正如savinger所指出的那样,你的代码中有很多功能,我真的不知道,所以我提供了一些注释来指出这个过程的位置:
// Start of your loop
// $value is what you're getting from the DB...
$array_search = array_search($value,$main_array);
if($array_search)
{
// If this value already exists, we need to add
// this value in +1 of its current value
$main_array[$array_search][] = $value + 1;
}
else
{
// Make a new array key and add in the value
$main_array[] = $value;
}
// End of your loop