Answer the question
In order to leave comments, you need to log in
How to concatenate 2 arrays in php?
The bottom line is this:
There are 2 arrays, the first one is simply numbered:
$clear = [];
for ($i = 1; $i <= 10; $i++) {
$clear[] = [
'mesto' => $i,
];
}
$mesta2 = \DB::table('mesta')->where('game_id', 595)->get();
Answer the question
In order to leave comments, you need to log in
Did so
$mesta2 = \DB::table('mesta')
->where('game_id', $gameId)
->join('users', 'mesta.user_id64', '=', 'users.id64')
->select(['mesta.*','users.username as username','users.avatar as avatar'])
->get();
$clear = [];
for ($i = 1; $i <= $game->mest_all; $i++) {
$find = 0;
foreach($mesta2 as $mestos) {
if($find == 0) {
if($mestos->mesto == $i) {
$find++;
$i++;
$clear[] = $mestos;
}
}
}
$clear[] = [
'mesto' => $i,
];
}
$mesta = json_encode($clear);
It fails because $mesta2 is a collection, not an array.
$clear = ...;
$mesta2 = ...;
$result = array_merge($clear, $mesta2->toArray())
$clear = [];
for ($i = 1; $i <= 10; $i++) {
$clear[] = [
'mesto' => $i,
];
}
$mesta2 = \DB::table('mesta')->where('game_id', 595)->get()->keyBy('mesto');
$result = \Illuminate\Support\Collection::make($clear)->map(function ($item) use ($mesta2) {
if ($persistedItem = $mesta2->get($item['mesto'])) {
return $persistedItem;
}
return $item;
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question