0

So I have array like this one.

[
    '0020098238 - Address 1' => [
        '20011045 - 1',
        '20011880 - 2',
        '20014727 - 3',
        '20015506 - 4'
    ],
    '0020011189 - Address 2' => [
        '20012490 - 11',
        '20018679 - 22',
        '20023569 - 33',
        '20028843 - 44'
    ],
    '0020102015 - Address 3' => [
        '20008315 - 55',
        '20008689 - 66',
        '20021267 - 77',
        '20032518 - 88'
    ]
]

Now, I want to make foreach or implode or something else and as result get list like this one

  • 0020098238 - Address 1
    • 20011045 - 1
    • 20011880 - 2
    • 20014727 - 3
    • 20015506 - 4
  • 0020011189 - Address 2
    • 20012490 - 11
    • 20018679 - 22
    • 20023569 - 33
    • 20028843 - 44
  • 0020102015 - Address 3
    • 20008315 - 55
    • 20008689 - 66
    • 20021267 - 77
    • 20032518 - 88

I have tried with foreach($array as $key => $val) and than implode $val but it doesnt work propper. I can collect keys but values from array missing.

Thx

Here is what I have tried (nested foreach)

foreach ($user_sap_data as $customer => $destinations) {
    echo '<ul>';
        echo '<li>' . $customer . '</li>';
        foreach ($destinations as $destination) {
            echo '<li>' . $destination . '</li>';
        }
    echo '</ul>';
}

but as result I am getting only key of first foreach (in nested foreach)

It is not the same question like PHP nested array into HTML list because in other question there are key => (key => val, key => val...) but in this example is key => (val, val, val) etc. its similair but not duplicated

5

4 Answers 4

0

Try with this solution. In second foreach you have to get key as value...

echo '<ul>';
foreach ($user_sap_data as $key => $value) {
 echo '<li>' . $key . '</li>';
 echo '<ul>';
  foreach ($value as $subkey => $subvalue) {
   echo '<li>' . $subkey . '</li>';
  }
 echo '</ul>';
}
echo '</ul>';

i think with implode doesnt work, but I didnt try :)

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

Comments

0
echo "<ul>";
foreach($arr as $key=>$values){
    echo "<li>$key</li>"; //print array key
    echo "<ul>";
    foreach($values as $val){ 
         echo "<li>$val</li>"; // print subarray values
    }
    echo "</ul>";
}
echo "</ul>";

Your Code - updated:

    echo '<ul>';
    foreach ($user_sap_data as $customer => $destinations) {
          echo '<li>' . $customer . '</li>';
          echo '<ul>';
          foreach ($destinations as $destination) {
            echo '<li>' . $destination . '</li>';
          }
          echo '</ul>';
        }
        echo '</ul>';

1 Comment

It is the same result, but I have figured it out, in second foreach I must $key => $val and take $key.... that was the problem... however thx
0

You can achieve this with two foreach loops one within another. First loop will iterate through Parent level elements, and second will iterate through child within it.

<?php
$array = array(
  '0020098238 - Address 1' => array(
    '20011045 - 1',
    '20011880 - 2',
    '20014727 - 3',
    '20015506 - 4'
  ),
  '0020011189 - Address 2' => array(
    '20012490 - 11',
    '20018679 - 22',
    '20023569 - 33',
    '20028843 - 44'
  ),
  '0020102015 - Address 3' => array(
    '20008315 - 55',
    '20008689 - 66',
    '20021267 - 77',
    '20032518 - 88'
  )
);
?>
<ul>
  <?php
  foreach ($array as $key => $val) {
    ?>
    <li>
      <?php echo $key; ?>
      <ul>
        <?php
        foreach ($val as $key_ch => $val_ch) {
          ?>
          <li>
            <?php echo $val_ch; ?>
          </li>  
          <?php
        }
        ?>
      </ul>
    </li>
    <?php
  }
  ?>
</ul>

Comments

0

Since you approached with implode, you can use like this:

echo "<ul>";
foreach ($arr as $key => $value) {
    echo "<li>$key";
    echo "<ul><li>" . implode("</li><li>", $value) . "</li></ul></li>";
}
echo "</ul>";

With two foreach nested:

echo "<ul>";
foreach ($arr as $key => $value) {
    echo "<li>$key";
    echo "<ul>";
    foreach ($value as $subvalue) {
        echo "<li>$subvalue</li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

Comments

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.