0

I have this array $pages which spits out this data:

Array ( 
    [Name] => Array ( 
        [Subname] => Array ( 
            [0] => 43.2057, -79.9632, 1, -70,-150 
            [1] => 140240757658.jpg 
            [2] => 5 
            [3] => 0 
        ) ) 
    [Name2] => Array ( 
        [Subname2] => Array ( 
            [0] => 43.1769, -79.4703, 5, -70,-150 
            [1] => 140267498933.png 
            [2] => 16 
            [3] => 0 
        ) ) 
)

and I have this foreach setup:

foreach($pages as $row => $value) {
    echo '<li>'.$row.'<ul>';
    foreach($value as $x => $y) {
        echo 
            '<li>
                <a href="page.php?" action='.str_replace(" ", "", strtolower($y[2])).'" title="'.$x.'">'.$x.'</a>
             </li></ul></li>';
    }
}

What I am trying to do is if [3] in each of the Subname is equal to 0, then skip it from my foreach.

NOTE: Subname and Name are just examples, they will be different for each one.

3
  • Shouldnt the actionparameter be page.php? what is the use of the action in your anchor? Commented Jun 13, 2014 at 18:01
  • If both [3] are equal to zero skip them ? Commented Jun 13, 2014 at 18:04
  • if one of [3] are equal to zero, skip that one. Commented Jun 13, 2014 at 18:05

3 Answers 3

1

This should work:

foreach ($pages as $page) {
    foreach($page as $subname) {
        if ($subname[3] != 0) {
            /* Do whatever you want with the data of this subname */
        } 
    }
}

Or this if you want to use the key names:

foreach ($pages as $pageKey => $page) {
    foreach($page as $subnameKey => $subname) {
        if ($subname[3] != 0) {
            /* Do whatever you want with the data of this subname */
        } 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

if($subname[3] === 0) continue; is going to be a much more efficient way of checking. It will check the value only once.
Why would it be more efficient? why would it check the value only once? The OP would then have to use an else in case the value is ≠ 0 am I wrong?.
0

If you want to skip the sub-foreach then this is the solution

foreach($pages as $row => $value) {
    if($value['Subname'][3] == 0)
        continue;

    echo '<li>' . $row . '<ul>';
    foreach($value as $x => $y) {
        echo '<li><a href="page.php?action=' . str_replace(" ", "", strtolower($y[2])) . '" title="' . $x . '">' . $x  . '</a></li></ul></li>';
    }
}

1 Comment

forgot to mention that Subname will be different
0

Since this is an Associative Array you will not be able to to check

$pages['foo']['bar'][0] === 0

before the first foreach begins since we don't know the value of $pages['foo'] before hand. when we get to the check you have already output html at the point in the form of:

 echo '<li>' . $row . '<ul>';

Comments

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.