-1

I have an array that I use a loop to run throught, the code below:

foreach($arry as $parentkey => $parentvalue){
  $secondloop = explode(",",$parentvalue);
  foreach($secondloop as $childvalue){
    echo $parentkey.' '.$childvalue ;
  }
}

When I run it, it does not display the parentkey. Does php not support that kind of loop? How do I make it display the parent key? What would be the best way to walk throught the loop to get the desired result?

original array

     Array ( [1] => 2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,172,173,174,181 [2] => 39,102,94,98,92,121 [3] => 45,77,117,103,109,99 [4] => 47,78,146,105,113,115,104 [5] => 48,79,106,114,120,110 [6] => 68,93,116,111,112 [7] => 140,150 [8] => 141,151 [9] => 143,144,166,153 [10] => 145,154,159 [11] => 157,155 [12] => 158,156 [13] => 160 [14] => 161 [15] => 162 [16] => 163 [17] => 164 ) 
5
  • 4
    I see nothing wrong with it, how does your original array look like? Commented Feb 10, 2013 at 10:47
  • i want to see the key of the parent array beside the exploded value Commented Feb 10, 2013 at 10:56
  • And what result do you get now? Commented Feb 10, 2013 at 10:57
  • explode is a string function, in your code the value is not a string, is that just a cut&copy mistake? Commented Feb 10, 2013 at 11:08
  • The nested loop is fine. The problem must be in your original array. Also change the echo to something like echo $parentkey.' '.$childvalue.'<br/>'; to make it easier to interpret the results Commented Feb 10, 2013 at 11:10

1 Answer 1

1

Given the information given, your code works, see the following cleaned up example.

<?php

$arry = array(
    1 => '2,3,10,11,27,28,35,36,165,37,38,40,41,42,43,44,46,49,50,61,62,65,66,75,67,71,69,72,73,74,76,96,90,91,97,107,118,147,119,122,139,142,148,149,168,169,170,171,17$
    2 => '39,102,94,98,92,121',
    3 => '45,77,117,103,109,99',
    4 => '47,78,146,105,113,115,104',
    5 => '48,79,106,114,120,110',
    6 => '68,93,116,111,112',
    7 => '140,150',
    8 => '141,151',
    9 => '143,144,166,153',
    10 => '145,154,159',
    11 => '157,155',
    12 => '158,156',
    13 => '160',
    14 => '161',
    15 => '162',
    16 => '163',
    17 => '164'
);

foreach($arry as $parentkey => $parentvalue){
        $secondloop = explode(",",$parentvalue);
        foreach($secondloop as $childvalue){
                echo 'Parent key: ' . $parentkey . ', child value: ' . $childvalue . PHP_EOL;
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@VictorKilo, no, the dump of the output clearly shows my kind of array structure. If the $parentvalue would not be a string, you would have way more index keys in the print_r dump.
Good call ;) It's late. I should sleep instead of trying to think.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.