Home » Php » php – making unequal array control

php – making unequal array control

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have an array ,vehicle owners and tools are available, there is no stability in the array there are three car owners,vehicles they own show. in short; I have 3 car owners and I wanted to show on the page which of these three vehicles are ford, male, scania

code it gives me
enter image description here

i want to do
enter image description here

<?php 
$vehicles = array(

    array(
     "vehicleowner" =>("john"),
     "vehicles" =>array(
     array(
      "id" =>(1),
      "model" =>("ford"),
      "brand"   =>(1830),   
     ),      
     array(
      "model" =>("scania"),
      "id" =>(2),
      "brand"   =>("r400"), 
     ),
     array(
      "id" =>(3),
      "model" =>("man"),
      "brand"   =>("tgx"),  
     )
     ),
     ),

    array(
     "vehicleowner" =>("alex"),
     "vehicles" =>array(
     array(
      "id" =>(1),
      "model" =>("ford"),
      "brand"   =>(1830),   
     ),      
     array(
      "id" =>(3),
      "model" =>("man"),
      "brand"   =>("tgx"),  
     )
     ),
     ),

     array(
     "vehicleowner" =>("Robert"),
     "vehicles" =>array(
     array(
      "id" =>(1),
      "model" =>("ford"),
      "brand"   =>(1830),   
     ),      
     array(
      "model" =>("scania"),
      "id" =>(2),
      "brand"   =>("r400"), 
     ),
     array(
      "id" =>(3),
      "model" =>("man"),
      "brand"   =>("tgx"),  
     )
     ),
     ),

 );

echo '<table border="1">
<thead>
<tr>
<td>vehicleowner</td>
<td>Ford</td>
<td>Scania</td>
<td>Man</td>
';

foreach($vehicles as $i => $value){
     // echo "<pre>";
    // print_r($value);

    $ford = $value["vehicles"][$i]["id"]==1;            

    if($ford){ $fordd = $value["vehicles"][$i]["brand"];}       

    $scania = $value["vehicles"][$i]["id"]==2;          
    if($scania){ $scaniaa = $value["vehicles"][$i]["brand"];}

    $man = $value["vehicles"][$i]["id"]==3;         
    if($man){ $mann = $value["vehicles"][$i]["brand"];}

?>

 <tr>
 <td><?php echo $value["vehicleowner"]?></td>
 <td><?php echo isset($fordd) ? $fordd : '-';?></td>
 <td><?php echo isset($scaniaa) ? $scaniaa : '-';?></td>
 <td><?php echo isset($mann) ? $mann : '-';?></td>

 <tr>


<?php } ?>
How to&Answers:

One simple way to do this, based on what you already have, would be

foreach($vehicles as $i => $value){
  $ford = $scania = $man = '-'; // initialize variables with placeholder value '-'
  foreach($value['vehicles'] as $vehicle) {
    // set variables to brand, if model matches
    if($vehicle['model'] == 'ford') {
      $ford = $vehicle['brand'];
    }
    if($vehicle['model'] == 'scania') {
      $scania = $vehicle['brand'];
    }
    if($vehicle['model'] == 'man') {
      $man = $vehicle['brand'];
    }
  }

?>

 <tr>
 <td><?php echo $value["vehicleowner"]?></td>
 <td><?php echo $ford;?></td>
 <td><?php echo $scania;?></td>
 <td><?php echo $man;?></td>
 <tr>

More sophisticated variants using an array instead of individual variables, etc., are of course conceivable.

Answer:

Add tags yourself

    $store = [];
    $unique = [];
    foreach ($vehicles as $object) {
        foreach ($object['vehicles'] as $vehicle) {
            $unique[$vehicle['model']] = true;
            $store[$object['vehicleowner']][$vehicle['model']] = $vehicle['brand'];
        }
    }
    foreach ($store as $owner => $cars) {
        echo $owner;
        foreach ($unique as $column => $_) {
            echo $cars[$column] ?? '-';
        }
    }

Answer:

@SisError thank you so much

echo $cars[$column] ?? '-'; 

quite successful
but

<td><?php echo $cars["scania"];?></td>  

error

enter image description here

$store = [];
$unique = [];
foreach ($vehicles as $object) {
    foreach ($object['vehicles'] as $vehicle) {
        $unique[$vehicle['model']] = true;
        $store[$object['vehicleowner']][$vehicle['model']] = $vehicle['brand'];
    }
}
foreach ($store as $owner => $cars) {
    $owner;
    foreach ($unique as $column => $_) {
        $cars[$column] ?? '-';
    }


?>

<tr>
<td><?php echo $owner?></td>
<td><?php echo $cars["ford"];?></td>
<td><?php echo $cars["scania"];?></td>
<td><?php echo $cars["man"];?></td>
<tr>


<?php } ?>

@misorude SisError thank you so much ,quite successful