Skip to content
Advertisement

making unequal array control

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 } ?>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement