php - Sort array object by value in Laravel
one text
Solution:
Try this.
class NewsController extends Controller
{
protected $news;
protected $newsService;
public function __construct(NewsTable $news, NewsServiceTable $newsService)
{
$this->news = $news;
$this->newsService = $newsService;
}
public function listAction(Request $request)
{
$param = $request->filter;
$data = $this->news->list();
foreach ($data as $key => $item) {
$service = $this->newsService->listById($item->news_id);
$data[$key]["service"] = $service;
}
$order_data = array();
$keyword = "home";
foreach ($data as $key => $item) {
if(array_search($keyword, array_column($item["service"], 'service_code')) !== false){
array_unshift($order_data, $item);
}else{
$order_data[] = $item;
}
}
return $order_data;
}
}
Source