user7982416
user7982416

Reputation:

Why is my foreach loop returning the entire array where it should be storing the individual values?

I have a string that looks like this:

JMOZ001-JMGMDM-2017-MAR-13-,JMOZ001-JMKJ-2017-MAR-15-,JMOZ001-JMGMKJ-2017-MAR-16-

I am using explode to create an array from each component separated by a comma using:

print_r($skusearchesarray[] = explode(",", $skusearches));

The ouput for the above is exactly as you would expect:

Array ( [0] => JMOZ001-JMGMDM-2017-MAR-13- [1] => JMOZ001-JMKJ-2017-MAR-15- [2] => JMOZ001-JMGMKJ-2017-MAR-16- )

Now I am trying to loop through each item in the array and do things for each one using this code:

foreach($skusearchesarray as $skusearchterm) {
    print_r($skusearchterm);
}

I expect the above to print out the string for each item in the array, but instead it prints out the whole array it is a part of. This is the output for that print:

Array ( [0] => JMOZ001-JMGMDM-2017-MAR-13- [1] => JMOZ001-JMKJ-2017-MAR-15- [2] => JMOZ001-JMGMKJ-2017-MAR-16- )

Same as printing out the array itself. What gives?

Upvotes: 1

Views: 62

Answers (3)

gview
gview

Reputation: 15421

Certainly your assignment was wrong, but that is actually not the only reason you are getting what you consider a confusing result.

As I commented, print_r is a debugging tool for the most part. Your code should just be echo each value in the foreach loop.

With that said, this line is problematic:

print_r($skusearchesarray[] = explode(",", $skusearches));

As pointed out in Patrick's answer, you should not be assigning the result to an array variable, but just a variable, that will become an array upon assignment.

So why did you not see this in your debugging? Because what you asked php print_r to do was to print_r the actual ASSIGNMENT ie. print_r($var = explode()) rather than the variable produced. In this case what ends up happening is that the result of the explode, which is the array you expect gets returned, fooling you into thinking that the new array variable is what you expected, when in fact the result is stuffed down in a sub-array element, which subsequently doesn't work right with your assumption about your foreach loop.

Hopefully this code will help you understand this more clearly, and illustrate how you got confused in the first place:

<?php

$skusearches = 'JMOZ001-JMGMDM-2017-MAR-13-,JMOZ001-JMKJ-2017-MAR-15-,JMOZ001-JMGMKJ-2017-MAR-16-';
print_r($skusearchesarray[] = explode(",", $skusearches));

print_r($skusearchesarray);

$skusearchesarray = explode(",", $skusearches);

print_r($skusearchesarray);

The results are:

Array
(
    [0] => JMOZ001-JMGMDM-2017-MAR-13-
    [1] => JMOZ001-JMKJ-2017-MAR-15-
    [2] => JMOZ001-JMGMKJ-2017-MAR-16-
)
Array
(
    [0] => Array
        (
            [0] => JMOZ001-JMGMDM-2017-MAR-13-
            [1] => JMOZ001-JMKJ-2017-MAR-15-
            [2] => JMOZ001-JMGMKJ-2017-MAR-16-
        )

)
Array
(
    [0] => JMOZ001-JMGMDM-2017-MAR-13-
    [1] => JMOZ001-JMKJ-2017-MAR-15-
    [2] => JMOZ001-JMGMKJ-2017-MAR-16-
)

Run the code here.

Upvotes: 1

Tosin John
Tosin John

Reputation: 524

Use echo instead of priint_r(), print_r() will print the whole array for you, provided you pass an array to it.

$skusearchesarray = explode(",", $skusearches)
foreach($skusearchesarray as $skusearchterm) {
    echo $skusearchterm;
}

Upvotes: 0

Patrick Di Martino
Patrick Di Martino

Reputation: 102

You are assigning the explode as second dimension of your array. change:

print_r($skusearchesarray[] = explode(",", $skusearches));

to:

print_r($skusearchesarray = explode(",", $skusearches));

Upvotes: 3

Related Questions