Parse output of Google Cloud command into PHP associative array This is my idea on how to turn Shell exec output in PHP to array output. I just want to share it and get feedback in case there is any better way.

Below is a code example to get server info from GCE.

$shell_command  = "gcloud compute instances list";
$result = shell_exec($shell_command);
//echo "\n$result\n";
$data = explode("\n",$result);
//print_r($data);
$headers = turn_linux_row_to_array($data[0]);
unset($data[0]);
$servers_array = array();
foreach($data as $row){
  $server_data = turn_linux_row_to_array($row,$headers);
   if(!empty($server_data)){
     $servers_array[] = $server_data;
   }
}
//function to move text out put to array
function turn_linux_row_to_array($row,$headers=array()){
$final_data_array = array();
$data_array_position = 0;
$row_length = strlen($row);
$current_word = '';
//echo "\nRow length is :: $row_length\n";
for($i=0; $i&lt;$row_length; $i++){
   $char = substr( $row, $i, 1 );
    if($char != ' '){
     //This is a part of a value
     $current_word .=$char;
    }else{
    if($current_word!=''){
       if(!empty($headers) &amp;&amp; isset($headers[$data_array_position]) ){
          $final_data_array[$headers[$data_array_position]] = 
    $current_word;
       }else{
         $final_data_array[] = $current_word;
       }
       $data_array_position++;
   }
   $current_word = '';
 }
}
//print_r($final_data_array);
return $final_data_array;
}
This code is complicated, and likely buggy.


If a line does not end in a space character, then the last word on that line will be ignored.
The output of gcloud compute instances list is a table.  For example:

NAME                                             ZONE          MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
gke-cluster-asia-eas-default-pool-dc8f484c-knbs  asia-east1-a  n1-standard-1               10.140.0.2   104.155.227.25  RUNNING
gke-cluster-asia-eas-default-pool-dc8f484c-x8cv  asia-east1-a  n1-standard-1               10.140.0.3   104.199.226.16  RUNNING
gke-cluster-asia-eas-default-pool-dc8f484c-z5wv  asia-east1-a  n1-standard-1               10.140.0.4   104.199.134.9   RUNNING


If the table contains an empty value (such as for the "PREEMPTIBLE" column above), then the turn_linux_row_to_array() function will ignore it, resulting in a mismatch between the headers and the values.


I don't recommend trying to improve this code.  Instead, noting that all Google Cloud commands accept a --format option, you should choose a format that is easy to parse.  I would recommend replacing the entire code with:

$servers = json_decode(shell_exec('gcloud compute instances list --format=json'), true);


… to take advantage of json_decode().  That's it — you're done!