我将2个数组与ArrayMerge函数合并,得到了 (dd函数结果)。
现在我想问的是,是否可以在(0 =>)元素内移动(2 => false)元素,在(1 =>)元素内移动(3 =>)false?如果有人知道该怎么做,请帮助我!
合并前第一个数组的内容
控制器laravel
//Listing clients from java application
public function DevicesGet(){
$route=file_get_contents("C:\Users\Sijan\Desktop\device27.01.2011\edit_path\edit_file.txt");
try{
$device= new Client();
$answer= $device->request('GET', $route.'devices');
$body = $answer->getBody();
$status = 'true';
$message = 'Data found!';
$final= json_decode($body);
$id_array = array();
foreach ($final as $res) {
// Add each id value in your array
$id_array[]= $res->clientId;
}
$a = array();
foreach($id_array as $my_id) {
$response2 = $client->request('GET', $path. 'devices/deviceAvailability/' . $my_id );
$a[] = json_decode($response2->getBody());
}
$path= array_merge($data, $a);
dd($path);
return view('devices.home', ['clients' => $path, 'status'=> $a]);
// is thrown for 400 level errors
}catch(ClientException $ce){
$status = 'false';
$message = $ce->getMessage();
$data = [];
//In the event of a networking error (connection timeout, DNS errors, etc.)
}catch(RequestException $re){
$status = 'false';
$message = $re->getMessage();
$data = [];
}//If some error occurs
catch(Exception $e){
$this->status = 'false';
$this->message = $e->getMessage();
$data = [];
}
Session::flash('error', 'Iist of devices is blank, because no connection !');
return view('devices.home', ['status'=>$status,'message'=>$message,'clients'=>$data]);
}
答案 0 :(得分:0)
您是否尝试过使用array_push
功能?
您应该能够简单地使用array_push($array, $valuetoinsert)
-您需要进行修改以考虑建立索引,但这应该是您想要的。
查看此Stackoverflow链接,也许它可能会提供更多帮助:How to add elements to an empty array in PHP?
希望这会有所帮助!
答案 1 :(得分:0)
对于您要执行的操作,您不需要$path = array_merge($data, $a);
从顶部循环执行,而使用$data
代替$path
。同样,当您将2和3移到对象中时,也要给它们一个键名,例如“ status”:
$a = array();
foreach($id_array as $key => $my_id) {
$response2 = $client->request('GET', $path. 'devices/deviceAvailability/' . $my_id );
$status = json_decode($response2->getBody());
$a[] = $status;
$data[$key]->status = $status;
}
return view('devices.home', ['clients' => $data, 'status'=> $a]);