0

Not sure what I'm doing wrong with this one, but when I attempt to do a foreach loop with a variable, it comes back with Array Array.

PHP Code:

  <?php foreach ($thing as $t) : ?>
    <?php $thisStuff = $t[stuff];  ?>
    <?php echo $thisStuff; ?>
  <?php endforeach; ?>

Output: Array Array

Could this be possibly that there is another array within $thisStuff? If so would I loop through that data?

4
  • 5
    Could this be possibly that there is another array within $thisStuff? Yes. print_r($thing); Commented Aug 14, 2017 at 17:49
  • I did and it comes back with a bunch of other arrays. So, how would I structure that data? That's what I'm confused about since I've never really worked with nested arrays. Commented Aug 14, 2017 at 17:51
  • Possible duplicate of php foreach with multidimensional array Commented Aug 14, 2017 at 18:02
  • 2
    Maybe update question with the print_r result. Commented Aug 14, 2017 at 18:08

2 Answers 2

1

If you have a multidimensional array you'll need to use a recursive function.

an example might be this

function unfoldArray($array,$output = array()) {
    if(is_object($array)) {
        $array = (array)$array;
    }
    foreach($array as $key => $value) {
        if(is_array($value) || is_object($value)) {
            //$output[] = $key;
            $output = unfoldArray($value,$output);
        } else {
            $output[] = $value;
        }
    }
    return $output;
}

the above function, given an array like

$array = array(
    "one",
    "two",
    "three" => array("A","B","C"),
    "four" => array("X","Y","Z"),
    "five",
    "six" => array(
        "sub_one",
        "sub_two",
        "sub_three" => array("sub_A","sub_B","sub_C")
    ),
    "seven"
);
$output = unfoldArray($array);

would return a flat array like this

   // $output 
   [
        "one",
        "two",
        "A",
        "B",
        "C",
        "X",
        "Y",
        "Z",
        "five",
        "sub_one",
        "sub_two",
        "sub_A",
        "sub_B",
        "sub_C",
        "seven"
    ]

you may notice that values "three" and "six" are omitted from the result array because they are key, but if you wan to include them just uncomment the line //$output[] = $key; in the function.

Once you have this array you may just loop with a foreach. This might not be exactly what you need, but should give you a direction to follow.

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

1 Comment

Awesome, thanks! Yeah, I was messing around with nesting a foreach loop in another foreach loop and while I'm getting the results it just seems really messy.
0

Try with key => value method:

  <?php foreach ($thing as $key => $value) : ?>
    // if $value is array, you need to iterate it the same way (additional foreach)
    <?php echo $value; ?>
  <?php endforeach; ?>

4 Comments

Works for an Associative array. Not a multi-dimensional array.
If it's multidimensional array, you need to use another foreach inside this one
to me, it sounds like he has an array of associative arrays...but yes @mitch i agree.
Yeah, there are a bunch of nested arrays within other arrays. Sheesh, this is going to be messy.

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.