Arrays are strange

I print out an array like

foreach($pduArray as $pdu) {
				if ($pduName !== $pdu['pdu_name']) {
					$pduName = $pdu['pdu_name'];
				   echo "<hr>";
				   echo '<h4 class="text-secondary">'.$pduName.'</h4>';
				   echo "\r\n";
				}
					echo '  <a href="show_recepticle.php?id='.$pdu['recepticle_id'].'" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$pdu['name'].'">';
					echo '<img src="../images/'.$pdu['type'].'.png" class="figure-img img-fluid rounded" alt="Recepticle.">';
					echo "</a>";
					echo "\r\n";
				}

Which results in…


<hr><h4 class="text-secondary">Luke's PDU.</h4>
  <a href="show_recepticle.php?id=1" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Foxtrot"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=2" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Chris Farley"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=3" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Promised Neverland"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=4" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Donald Trump"><img src="../images/6.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=5" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Luke Urtnowski"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=7" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Loner"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
<hr><h4 class="text-secondary">Wally's PDU.</h4>
  <a href="show_recepticle.php?id=6" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Peter Griffin"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=9" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Rick & Morty"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=10" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Desert Eagle"><img src="../images/5.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=11" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Tango"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=12" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Patriot"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
<hr><h4 class="text-secondary">Fred's PDU.</h4>
  <a href="show_recepticle.php?id=13" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="The Renegade"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=14" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Eric Andre"><img src="../images/7.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=15" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Decker"><img src="../images/4.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
<hr><h4 class="text-secondary">Ian's PDU.</h4>
  <a href="show_recepticle.php?id=8" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Coronado"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=16" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Nathan Explosion"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
<hr><h4 class="text-secondary">Thor's PDU.</h4>
  <a href="show_recepticle.php?id=17" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Phil Hartman"><img src="../images/1.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
<hr><h4 class="text-secondary">Pete's PDU.</h4>
  <a href="show_recepticle.php?id=18" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Joe Rogan"><img src="../images/2.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=19" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Dan Crenshaw"><img src="../images/5.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=20" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Will Ferrell"><img src="../images/3.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>
  <a href="show_recepticle.php?id=21" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="Hannibal Burress"><img src="../images/9.png" class="figure-img img-fluid rounded" alt="Recepticle."></a>

which is good as it groups the PDUs…
Niw, imm trying to only print the part of the array at [0],[1],[3] separately
Heres what I tried

$index = 0;
foreach($pduArray[$index] as $pdu) {
echo '<a href="show_recepticle.php?id='.$pdu['recepticle_id'].'" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$pdu['name'].'">';
echo '<img src="../images/'.$pdu['type'].'.png" class="figure-img img-fluid rounded" alt="Recepticle.">';
echo "</a>";
echo "\r\n";
$index++;
}

but am getting

Warning: Illegal string offset ‘recepticle_id’ in C:\xampp\htdocs\DCT\2\pdus\name_list_tabs.php on line 44

PHP function foreach expects an array.

https://www.php.net/manual/en/control-structures.foreach.php

Try the following:

echo '<br>$index ==> ' .gettype( $index );
echo '<br>$pduArray ==> ' .gettype( $pduArray );
echo '<br>$pduArray[$index]  ==> ' .gettype( $pduArray[$index] );

foreach($pduArray as $index => $pdu) {
  echo '<br>' .$index] .' ==> ' .$pdu ;
}

If your array only contains 0,1 and 3 then that will indeed fail as your code will also try to render key 2, but that doesn’t exist.

Seems you’re looking for this:

foreach($pduArray as $index => $pdu) {
echo '<a href="show_recepticle.php?id='.$pdu['recepticle_id'].'" class="jqeasytooltip" data-tiptheme="tipthemewhite"  data-tipcontent="'.$pdu['name'].'">';
echo '<img src="../images/'.$pdu['type'].'.png" class="figure-img img-fluid rounded" alt="Recepticle.">';
echo "</a>";
echo "\r\n";
}

Note the foreach loop: foreach($pduArray as $index => $pdu)

Basically tells PHP to loop over the array and foreach key/value assign the key to $key and the value to $pdu.

I’m guessing that what you are asking for is not really what you want but:

$indexes = [0,1,3];
foreach($indexes as $index) {
    $pdu = $pduArray[$index];
    echo $pdu['name'] . \n";
}

You need to understand that $pduArray is actually an array of arrays and that it makes little sense to foreach over a $pdu array.

You also need to be aware that PHP has a number of control structures besides foreach. Structures such as for or perhaps while might be what you are looking for,

1 Like

…and it is good to check if array_key_exists before attempting to use it.

if(array_key_exists($index,$pduArray)): 
	$pdu = $pduArray[$index];
	echo $pdu['name'] . "\n";
endif;

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.