Skip to content
Advertisement

get key of multi dimension array (PHP Language)

Hi I have a problem regarding multi-dimension array. I have a set of array looking like this

array:3 [▼
  0 => array:1 [▼
    "tree" => "0"
  ]
  1 => array:1 [▼
    "tree" => "2"
  ]
  2 => array:1 [▼
    "tree" => "0"
  ]
]

the array is from

dd($this->treeArray);

how do I want to get the array which the value is 0. Expected result/filter For example:

array:2 [▼
  0 => array:1 [▼
    "tree" => "0"
  ]
  1 => array:1 [▼
    "tree" => "0"
  ]
]

or 

array:1 [▼
  0 => array:1 [▼
    "tree" => "2"
  ]
]

if changing the index is impossible, its ok.. Thank you

Advertisement

Answer

As you are using Laravel, you could use array helper such as this

You can use like this:

use IlluminateSupportArr;

$newTreeArray = Arr::where($this->treeArray, function ($tree) => $tree['tree'] === "0");
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement