Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a array which has string and array.

I am trying to get the all the values with foreach but it does not work. It seems simple but I am stuck with this. If anybody can tell me where to fix, that would be very great. Thank you in advance.

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

    }
}

?>
share|improve this question
    
I am trying to get the all the values what do you want at the end? –  Cheery Oct 21 at 4:17
    
What does "does not work" mean in this context? What's the behavior you're trying to correct? –  Jon Kiparsky Oct 21 at 4:18
    
I want to display all the values of the each array. –  user3011308 Oct 21 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 –  Jon Kiparsky Oct 21 at 4:20
    
I can get display the strings in the array but cannot be shown the array. –  user3011308 Oct 21 at 4:21

4 Answers 4

up vote 0 down vote accepted

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

    }

}
share|improve this answer

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){
share|improve this answer

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.

share|improve this answer
    
Thank you for kindly helping me to underdstand. –  user3011308 Oct 21 at 4:51

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.