1

I use PHPExcel to put elements of my Excel file into an array. I then do

print_r($worksheets); 

Where $worksheets is the array created through PHPExcel. The output is

Array ( 
    [enquiry] => Array ( 
        [0] => Array ( 
            [0] => 86.141.247.93 
        ) 
        [1] => Array ( 
            [0] => 188.141.76.143 
        ) 
        [2] => Array ( 
            [0] => 2.29.20.161 
        ) 
    ) 
)

What I need to do is pass each of these IPs as a String to a function. So what I am trying is this

foreach($worksheets as $ip) {
    $count = 0;
    if ($SpamProtecter->CheckIP($ip[$count][0])) {
        print_r("SPAM");
    } else {
        print_r("GOOD");
    }
    $count++;
}

The problem I have is that it only prints out one result. How can I pass each array element as a String to CheckIP?

Thanks

4 Answers 4

3

You have an intermediate level in your array. Remember, foreach iterates over the top level of elements. You have 3 tiers here

foreach($worksheets['enquiry'] as $ip) { //iterate tier 1
    if ($SpamProtecter->CheckIP($ip[0])) { //grab tier 3
        print_r("SPAM");
    } else {
       print_r("GOOD");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your $worksheets array only has a single key: enquiry, which is why you're only getting a single result output. Try this:

foreach($worksheets['enquiry']) as $ip) {
    if($SpamProtector->CheckIP($ip[0]) {
    // ...

I think you can also get rid of that inner $count variable since it is not used anymore.

Comments

2

This could work

foreach($worksheets['enquiry'] as $ip) {

    if ($SpamProtecter->CheckIP($ip[0])) {
        print_r("SPAM");
    } else {
        print_r("GOOD");
    }

}

Comments

1

array_walk works well in situtations where you want to perform an action on each element in an array.

array_walk($worksheets["enquiry"], 
           function ($a) use ($SpamProtecter) { 
                echo $SpamProtecter->CheckIP($a[0])?"GOOD":"BAD";
           });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.