mardi 17 juillet 2018

Echo array inside foreach and if isset but only show one data for each value

So I have this list of array (I changed the values for privacy purpose);

$datalist  = array(
1 => 
array(
     "12.2.2.2",
     "Netherlands 1",
     "Hub 1",
     "Password",
     "Mfalse"
) ,
array(
    "12.2.2.2",
    "Singapore 1",
    "Hub 2",
    "Password 2,
    "Mfalse"
) ,

And I want the output of array[2] which is the 'Hub' and 'Hub 2'. So I did this php code since I want it to be looped inside a table so all the details will be tabled and organized. But the only thing I need for now is the HUB to be shown.

                foreach ($response->monitors as $monitors) {
                    if(isset($monitors->id)){
                      echo
                      '<tr>
                      <td><i class="fas fa-globe"></i> '.$monitors->friendly_name.'</td>';
                           foreach ($datalist as $key => $value) {
                            # code...
                            if (isset($key)) {
                               echo '<td>'.$value[2].'</td>';
                            }
                        }
                      echo '<td>'.$monitors->url.'</td>
                      <td>'.$monitors->status.'</td>
                      </tr>';
                    }
                  }

Since i'm also using other value outputs and the array I wanted is also inside another foreach loop for the table.

So the main problem lies in this part, since whenever I use this, it outputs like this on each loop of the table

foreach ($datalist as $key => $value) {
                        # code...
                        if (isset($key)) {
                           echo '<td>'.$value[2].'</td>';
                        }
                    }

Output:

Netherlands 1 Hub 1 Hub 2 12.2.2.2 
Singapore   1 Hub 1 Hub 2 12.2.2.2 

But the desired output i'm looking for is;

Netherlands 1 Hub 1 12.2.2.2 
Singapore   1 Hub 2 12.2.2.2 

What could be wrong in my array statement for foreach?

Aucun commentaire:

Enregistrer un commentaire