Reputation: 203
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 array2 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?
var_dump of $datalist
array (
1 =>
array (
0 => 'x',
1 => 'Netherlands 1',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
2 =>
array (
0 => 'x',
1 => 'Singapore 1',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
3 =>
array (
0 => 'x',
1 => 'Singapore 2',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
4 =>
array (
0 => 'x',
1 => 'Singapore 3',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
5 =>
array (
0 => 'x',
1 => 'Singapore 4',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
6 =>
array (
0 => 'x',
1 => 'Singapore 5',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
7 =>
array (
0 => 'x',
1 => 'Singapore 6',
2 => 'PeenoiseSync',
3 => 'x',
4 => 'Mfalse',
),
8 =>
array (
0 => 'x',
1 => 'Singapore 7',
2 => 'YTAC',
3 => 'x',
4 => 'Mfalse',
),
)
Updated again: Desired output should be something similar to this;
Upvotes: 0
Views: 1580
Reputation: 6393
This answer assumes that your $datalist
array is in the same order as your $monitors
array and contains the same number of elements. You simply need to keep track of which monitor you're on, and then use the same index to access the corresponding $datalist
row. You do this by setting as $index=>$monitors
and then using $datalist[$index][2]
// re-indexing $datalist since in your example it is not 0-indexed
$datalist = array_values($datalist);
foreach ($response->monitors as $index=>$monitors) {
if(isset($monitors->id)){
echo
'<tr>
<td><i class="fas fa-globe"></i> '.$monitors->friendly_name.'</td>';
echo '<td>'.$datalist[$index][2].'</td>';
echo '<td>'.$monitors->url.'</td>
<td>'.$monitors->status.'</td>
</tr>';
}
}
Upvotes: 2
Reputation: 54
You will need a variable to compare from $monitors against $value[1] since is going to output all arrays information in $datalist per $monitors iteration, I'm leaving an example on how you can solve this, but in order to help you further I will need to know what $monitors->friendly_name exactly outputs and what you have in the $monitors variable (maybe there's something we can use to compare and match your $datalist array corresponding to the correct monitor:
foreach ($datalist as $key => $value) {
# code...
if ($monitors->friendly_name == $value[1]) {
echo '<td>'.$value[2].'</td>';
}
Assuming that $key is always set (I don't think foreach will not set the $key in an iteration), this will output the value matching the correct array value for that specific monitor
Upvotes: 0
Reputation: 21
You miss an " after password 2
$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"
)
);
foreach ($datalist as $key => $value) {
# code...
if (isset($key)) {
echo '<td>'.$value[2].'</td>';
}
}
test here
Upvotes: 0