0

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;
    }
}

?>
5
  • I am trying to get the all the values what do you want at the end? Commented Oct 21, 2014 at 4:17
  • What does "does not work" mean in this context? What's the behavior you're trying to correct? Commented Oct 21, 2014 at 4:18
  • I want to display all the values of the each array. Commented Oct 21, 2014 at 4:18
  • And what is happening that you don't want to have happen? Tell us what problem you want fixed, and you're likely to get a better answer Commented Oct 21, 2014 at 4:20
  • I can get display the strings in the array but cannot be shown the array. Commented Oct 21, 2014 at 4:21

4 Answers 4

1

I believe the problem lies in this line

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

since the array inside of the "img" key is not a key value pair, this line will fail. What you want is:

foreach ($row as $rowChild){
1

In your code, $row stands for:

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

So $rowChild stands for "Large", array("images/nike1.jpg","images/nike2.jpg","images/nike3.jpg") and "$30" in the loop. And in your second loop, $j is not int index but string index ("size", "img" and "price").

When you try to print the array, it breaks.

1
  • Thank you for kindly helping me to underdstand. Commented Oct 21, 2014 at 4:51
0

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>';
        }

    }

}
0
0

If you're getting an array to string conversion error, I would look at the line where that error is likely to be triggered:

  echo  $rowChild; 

If $rowChild is an array, echoing it isn't going to work. For example, if it's an array of image filenames

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.