PHP foreach loop through table that has a string and a table

advertisements

I have an array which has string and array.

I am trying to get the all the values with foreach. Why does it not work?

<?php

$shirts = array();

$shirts[101] = array(
    "size"  =>  "Large",
    "img" =>  array("images/nike1.jpg","images/nike2.jpg","images/nike3.jpg"),
    "price" => "$30";
);

$shirts[102] = array(
    "size"  =>  "Small",
    "img" =>  array("images/adidas1.jpg","images/adidas2.jpg","images/adidas3.jpg"),
    "price" => "$30";
);

$shirts[103] = array(
    "size"  =>  "Medium",
    "img" =>  array("images/puma1.jpg","images/puma2.jpg","images/puma3.jpg"),
    "price" => "$30";
);

$last = count($shirts) - 1;

foreach ($shirts as $i => $row){
    $isFirst = ($i == 0);
    $isLast = ($i == $last);

    if (is_array($row)){
        $lastChild = count($row) - 1;
        foreach ($row as $j =>$rowChild){
            $isFirstChild = ($j == 0);
            $isLastChild = ($j == $lastChild);                          

            echo  $rowChild;
        }
    }else{
        echo  $row;
    }
}

?>


Your foreach loop should be like this:

foreach( $shirts as $i=>$row ){

    foreach( $row as $j=>$product ){

        if( is_array( $product ) ){
            foreach( $product as $p ){
                echo $p.'<br>';
            }
        } else {
            echo $product.'<br>';
        }

    }

}