我有9个街区,我希望按最后添加的新闻排序。 我有代码:
$projects = Projects::find(['conditions' => 'active = 1', 'order' => 'id DESC']);
$itemsps = [];
foreach($projects as $project) {
if(!$project->{'link_' . $lang. ''}) continue;
$itemsp['title_md'] = $project->title_md;
$itemsp['title_ru'] = $project->title_ru;
$itemsp['link'] = 'http://'.$_SERVER['SERVER_NAME']. '/' . $lang . '/' . $project->{'link_' . $lang. ''};
$category = Categories::findFirst('alias = ' . "'$project->link_md'" . ' OR alias_ru = ' . "'$project->link_ru'");
$lastCat = NewsCategories::find('categories_id = ' . $category->id)->getLast();
if($lastCat === false) continue;
$lastImage = 'uploads/' . $lastCat['news_id'] . '.jpg';
$itemsp['image'] = $lastImage;
$itemsps[] = $itemsp;
}
我如何按以下记录订购:
$lastCat = NewsCategories::find('categories_id = ' . $category->id)->getLast();
我的阵列中的结果:
itemsps
答案 0 :(得分:0)
我不知道你的数据库,但我敢打赌,只需一个查询就可以实现你的目标,并且有些表可以加入。 请花点时间了解phalcon models
如果您仍想使用该格式,可以使用array_multisort()
对数组进行排序:
$projects = Projects::find(['conditions' => 'active = 1', 'order' => 'id DESC']);
$itemsps = [];
foreach($projects as $project) {
if(!$project->{'link_' . $lang. ''}) continue;
$itemsp['title_md'] = $project->title_md;
$itemsp['title_ru'] = $project->title_ru;
$itemsp['link'] = 'http://'.$_SERVER['SERVER_NAME']. '/' . $lang . '/' . $project->{'link_' . $lang. ''};
$category = Categories::findFirst('alias = ' . "'$project->link_md'" . ' OR alias_ru = ' . "'$project->link_ru'");
$lastCat = NewsCategories::find('categories_id = ' . $category->id)->getLast();
$itemsp['lastCat'] = $lastCat; //ADD THIS
if($lastCat === false) continue;
$lastImage = 'uploads/' . $lastCat['news_id'] . '.jpg';
$itemsp['image'] = $lastImage;
$itemsps[] = $itemsp;
}
//ADD THE CODE BELOW
$lastCat_array = array();
foreach ($itemsps as $key => $row)
{
$lastCat_array[$key] = $row['lastCat'];
}
array_multisort($lastCat_array, SORT_DESC, $itemsps);
print_r($itemsps);