0

I have an array result like this.

Array
(
    [Bank] => Array
        (
            [name] => Bank
            [status] => 1
            [enable_rcb] => 1
        )

    [Cod] => Array
        (
            [name] => Cod
            [status] => 1
            [enable_rcb] => 0
        )

    [Lite] => Array
        (
            [name] => Lite
            [status] => 0
            [enable_rcb] => 0
        )
)

I want to get result from conditional. For example I want show only Bank if the status is 1 and leave the rest array. I try to show them like this

        $results = array();
        foreach ($user_pay as $value) {
            foreach ($value as $k => $v) {
                if($value['status']=="1"){
                    $user_payment[] = $value['name'];
                }
            }
            $results = $user_payment;
        }

But the result give me duplicate result like this

Array
(
    [0] => Bank
    [1] => Bank
    [2] => Bank
    [3] => Bank
    [4] => Bank
    [5] => Bank
    [6] => Cod
    [7] => Cod
    [8] => Cod
    [9] => Cod
    [10] => Cod
)

What I want is to get the name of an array which is have status 1 and it should be like this

Array
(
    [0] => Bank
    [1] => Cod
)

How to avoid this duplicate result?

  • 1
    Does this answer your question? [How to search by key=>value in a multidimensional array in PHP](https://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – BhAvik Gajjar Dec 11 '19 at 06:57
  • After editing my answer below, I realized something weird: how come you can have duplicates if the names are also your array's keys? You can't have the same twice. – Jeto Dec 11 '19 at 07:23
  • Your inner loop can just be removed `foreach ($value as $k => $v) {` - you don't even use the values from this loop! – Nigel Ren Dec 11 '19 at 07:56

4 Answers4

3

You may use array_filter to retrieve only the ones with the desired status, then array_column to grab their names:

$filtered = array_filter($arr, static function ($entry) {
  return $entry['status'] === 1;
});

$names = array_column($filtered, 'name');

Demo: https://3v4l.org/Nhc0P

Jeto
  • 14,596
  • 2
  • 32
  • 46
  • thanks for help, but this result as null value and give me a blank page – GregoriRasputin Dec 11 '19 at 08:42
  • @GregoriRasputin As you can check in the provided link, this works fine unless you're on PHP 5.5.0 or below (in which case, I highly recommend you upgrade). – Jeto Dec 11 '19 at 08:50
  • yes, I'm on php 5 right now, Upgrade means change all code in my file? – GregoriRasputin Dec 11 '19 at 09:00
  • @GregoriRasputin No I mean upgrading to PHP7. This can take a bit of time depending on your codebase, but you'll get way better performance, stability, and features. PHP5 isn't even supported anymore. (And if you absolutely can't, at the very least upgrade to PHP5.6, this answer will work fine with it.) – Jeto Dec 11 '19 at 09:10
  • Thanks. You give me best suggestion. I will update it later. – GregoriRasputin Dec 11 '19 at 11:02
2

You can change loop such that

$results = array();
foreach($array as $key=>$value):
    if($value["status"]){
      $results[] = $value["name"];
    }
endforeach;
Bharat Godam
  • 477
  • 1
  • 5
  • 18
1

You can approach it like

$new = array_keys(array_filter($a,function($v){
  return $v['status'] > 0;
}));

print_r($new);

Working example :- https://3v4l.org/5rQjm

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
-1

You can use array_unique()

$result = array_unique($result);
Fariz Aliyev
  • 119
  • 3