0

I'm currently building an array from a sql query

$displayResult = $mysqlConn->query($getDisplays);

$displayNames = array();
foreach($displayResult as $subArray) {
  if(!array_key_exists($subArray['location_name'], $displayNames)) {
    $displayNames[$subArray['location_name']] = array();
  }
    $displayNames[$subArray['location_name']][] = $subArray['display_name'];
}

print_r($displayNames);

And it prints like:

Array
(
[Office 1] => Array
    (
        [0] => lobby
        [1] => break room
    )

[Office 2] => Array
    (
        [0] => lobby
        [1] => break room
    )

But for some reason when I loop and try to echo the array key as a header and the children as links, it just dumps the word 'array'

<?php foreach($displayNames as $key => $displayName):?>
<h2><?php echo $key; ?></h2>
<a><h4><?php echo $displayName; ?></h4></a>
<?php endforeach;?>

I tried dumping by index but it's blank. Do I need another nested foreach to get each child?

Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • `$displayName`is an array, no? – Mickaël Leger Jul 18 '18 at 12:58
  • $displayNames is, but $displayName should be acting as the value here, right? – Geoff_S Jul 18 '18 at 13:00
  • And that value is what? Exactly, an _array_ containing the two entries `lobby` and `break room` … – CBroe Jul 18 '18 at 13:01
  • no. displayName in this loop contains the array that contains `0 => lobby, 1 => break room`... your $key actually contains Office 1 and Office 2 (on next loop) – Craftein Jul 18 '18 at 13:01
  • Possible duplicate of [The word "Array" gets printed instead of the values of the rows](https://stackoverflow.com/questions/6398623/the-word-array-gets-printed-instead-of-the-values-of-the-rows) and https://stackoverflow.com/q/3382473/2943403 and https://stackoverflow.com/q/12979722/2943403 and https://stackoverflow.com/q/30956106/2943403 and https://stackoverflow.com/q/14349100/2943403 – mickmackusa Jul 18 '18 at 13:18

4 Answers4

1

You have multidimensional / nested array, so $displayName is actually also array, and you try to print it as a string. Just call its children using their indexes. The simplest solution would be:

<?php foreach($displayNames as $key => $displayName):?>
<h2><?php echo $key; ?></h2>
<a><h4><?php echo $displayName[0] . '-' . $displayName[1]; ?></h4></a>
<?php endforeach;?>

Or for multiple elements, you can use the for loop or foreach

  <?php foreach($displayNames as $key => $displayName):?>
    <h2><?php echo $key; ?></h2>
      <?php foreach($displayName as $key2 => $displayNameRoom): ?> 
       <a><h4><?php echo $displayNameRoom; ?></h4></a>
      <?php endforeach;  ?>      
    <?php endforeach;?>
Nikola Kirincic
  • 3,651
  • 1
  • 24
  • 28
1

Your array looks like this :

Array
(
  [Office 1] => Array
  (
    [0] => lobby
    [1] => break room
  )

  [Office 2] => Array
  (
    [0] => lobby
    [1] => break room
  )

So in your foreach loop when you do :

<?php foreach($displayNames as $key => $displayName):?>
  <h2><?php echo $key; ?></h2>
  <a><h4><?php echo $displayName; ?></h4></a>
<?php endforeach;?>

The $displayName is an array.

To get "lobby" or "break room" value, you should do $displayName[0] for "lobby" and $displayName[1] for "break room".

Edit :

If in the future you have more than 2 values, two solutions :

foreach ($displayName as $value) {
    echo $value; // you will get "lobby", then "break room", then the other value
}

or

$i = 0;
do {
    echo $displayName[$i];
    $i++;
} while (isset($displayName[$i]));

Same result as before, you just echo each value while there is a value !

Hope it helps

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
  • But if later elements have more than just 2 children, can't I loop on those so that it will build for how ever many I hage? – Geoff_S Jul 18 '18 at 13:01
  • In other words, what if other offices have lobby, break room and converence room or even more than that? – Geoff_S Jul 18 '18 at 13:02
0

The exact output you're after.

Basically your $displayName contains an array which are lobby and break room.

<?php foreach ($displayNames as $location): ?>
    <?php foreach ($location as $displayKey => $displayName): ?>
        <h2><?= $displayKey ?></h2>
        <a><h4><?= $displayName ?></h4></a>
    <?php endforeach; ?>
<?php endforeach; ?>
Craftein
  • 762
  • 5
  • 10
-1

simply do:

<?php foreach($displayNames as $key => $displayName):?>
<h2><?php echo $displayNames[$key]; ?></h2>
<a><h4><?php echo $displayName; ?></h4></a>
<?php endforeach;?>