ymakux
ymakux

Reputation: 3485

The simplest question: extract values from array

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

Please help php-beginner :) Thanks!

Upvotes: 3

Views: 185

Answers (5)

Artefacto
Artefacto

Reputation: 97805

print_r(
    array_map(function ($a) { return $a["title"]; },
        array_filter($original,
            function ($a) { return $a["checkout"] && !$a["no_gateway"]; }
        )
    )
);

Upvotes: 3

Kendall Hopkins
Kendall Hopkins

Reputation: 44104

foreach( $array as $value ) {
    if( $value["checkout"] == 1 && $value["no_gateway"] == 0 ) {
        print $value["title"].PHP_EOL;
    }
}

Upvotes: 2

Ryan Tenney
Ryan Tenney

Reputation: 1841

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

Upvotes: 2

GSto
GSto

Reputation: 42350

foreach ($items as $item) {
  if($item['checkout'] == 1 && $item['no_gateway'] == 0) {
      echo $item['title'];
  }
}

assuming your array is called $items

Upvotes: 4

Jamie Wong
Jamie Wong

Reputation: 18350

foreach($array as $row) {
  if ($row['checkout'] && !$row['no_gateway']) {
    print $row['title'];
  }
}

Upvotes: 9

Related Questions