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?

5

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
In other words, what if other offices have lobby, break room and converence room or even more than that?
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;?>

2 Comments

This is exactly what I was needing, so I initially had the right approach but I see where I went wrong in indexing the nested foreach. Thank you so much
@TomN., no probs :)
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; ?>

Comments

-1

simply do:

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

1 Comment

This just prints Array for each key and value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.