0

I merged 2 arrays with the function ArrayMerge and I get this (dd function Result).

Now i want to ask is it possible to movee( 2=> false) element inside (0=>) element and (3=>) false inside (1=>) element ? If someone know how to do that, please help me !

Content of first array before merging

enter image description here

Controller 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]);
}
Nenad
  • 323
  • 2
  • 6
  • 21
  • Why put an image of text? Paste the text \in the question. This is way we can copy it to build a test of your code and help easier [mcve] – Nic3500 Jul 19 '18 at 22:11
  • First image is not of text, this is image of dd function. And i put my code from controller which you can use it. – Nenad Jul 19 '18 at 22:20
  • If I can read it, it is text. To test your code I would have to type in the input data based on your images. oh well, someone else might have the patience. – Nic3500 Jul 19 '18 at 22:23
  • Ok,really sorry it my bad.I updated my question.Can you help me ? – Nenad Jul 19 '18 at 22:34

2 Answers2

0

Have you tried using the array_push feature?

You should be able to simply use array_push($array, $valuetoinsert) - You'll need to modify to account for indexing, but this should be what you're looking for.

Check out this Stackoverflow link, and perhaps it may be of more assistance: How to add elements to an empty array in PHP?

Hope this helps!

TecHalo
  • 51
  • 9
0

For what you are trying to do you don't need $path = array_merge($data, $a); do it from the top loop and use $data in place of $path. Also you would want to give the 2 and 3 a key name like 'status' when you move them into to objects:

    $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]);
Nenad
  • 323
  • 2
  • 6
  • 21
Kubwimana Adrien
  • 2,463
  • 2
  • 8
  • 11
  • Thanks for answer but i used your code and i'm getting syntax error, unexpected '=>' (T_DOUBLE_ARROW) – Nenad Jul 20 '18 at 11:33
  • I have updated the answer. I made a typo change this ``$data[$key]->status => $status;`` to ``$data[$key]->status = $status;`` – Kubwimana Adrien Jul 23 '18 at 03:19