views:

97

answers:

5

So this is an example:

Array ( 
[0] => Array ( [title] => Title_1 [checkout] => 1 [no_gateway] => 0 ) 
[1] => Array ( [title] => Title_2 [checkout] => 1 [no_gateway] => 1 )
[2] => Array ( [title] => Title_3 [checkout] => 0 [no_gateway] => 0 )
[3] => Array ( [title] => Title_4 [checkout] => 1 [no_gateway] => 1 )
[4] => Array ( [title] => Title_5 [checkout] => 0 [no_gateway] => 0 )
[5] => Array ( [title] => Title_6 [checkout] => 1 [no_gateway] => 0 )
)

I need to print out all values under [title] key having [checkout] => 1 & [no_gateway] => 0

In my case it should looks like

  • Title_1
  • Title_6

Please help php-beginner :) Thanks!

+9  A: 
foreach($array as $row) {
  if ($row['checkout'] && !$row['no_gateway']) {
    print $row['title'];
  }
}
Jamie Wong
+4  A: 
foreach ($items as $item) {
  if($item['checkout'] == 1 && $item['no_gateway'] == 0) {
      echo $item['title'];
  }
}

assuming your array is called $items

GSto
+2  A: 

You tagged the question with the answer: foreach

// assuming $arr is the array containing the values from the example
foreach ($arr as $record) {
    if ($record['checkout'] && !$record['no_gateway']) {
        echo $record['title'], "\n";
    }
}
Ryan Tenney
+2  A: 
foreach( $array as $value ) {
    if( $value["checkout"] == 1 && $value["no_gateway"] == 0 ) {
        print $value["title"].PHP_EOL;
    }
}
Kendall Hopkins
+3  A: 
print_r(
    array_map(function ($a) { return $a["title"]; },
        array_filter($original,
            function ($a) { return $a["checkout"] && !$a["no_gateway"]; }
        )
    )
);
Artefacto
Hot damn, I didn't know you could declare lambda functions like that in php. No more stupid `create_function` calls or passing in functions as a string of their name.
Jamie Wong
@Jam It's a new feature in PHP 5.3. See http://pt.php.net/manual/en/functions.anonymous.php
Artefacto
@evolve different things.
Artefacto
Yep, thanks. Went to read about it after I saw this post. I saw it in a php web framework but thought they were using some hack.
Jamie Wong
Downvoted due to complexity of answer. The person asking this question was a complete newbie and this solution is definitely overkill.
Ian Silber
@Ian The complexity is in your head. The reason it might seem complicated is because the syntax in PHP for lambda functions is quite verbose. If you could define one like in Mathematica `#^2 }`, it would not seem so "complex".
Artefacto
@Artefacto - you missed my point. The fact that have an anonymous (or lambda) function nested inside two additional functions is not easily comprehendible to newbies. That'd be like teaching a kindergartner long division to solve a problem that be solved with simple subtraction.
Ian Silber
@Ian That's condescending to the OP, who, by the way, is not the only target of this question; it's meant to be here for the posterity. Nevertheless, this is Programming 101 and is, in fact, a straightforward implementation: filter the wanted elements, transform the remaining ones.
Artefacto
@Artefacto Not trying to be condescending to the original poster, trying to help him. He claimed himself "Please help php-beginner :)"
Ian Silber