Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter this multidimensional array?

Tags:

php

This is my input array:

INPUT :

$input["a"]["b"]["UK"] = 96 ;
$input["a"]["c"]["UK"] = 69 ;
$input["a"]["bp"]["USA"] = 29 ;
$input["a"]["c"]["USA"] = 59 ;
$input["a"]["dd"]["UK"] = 31 ;
$input["a"]["cg"]["UK"] = 38 ;

I want to get a new array that will contain only ["USA"]. So output have to be..............

OUTPUT :

$output["a"]["bp"]["USA"] = 29 ;
$output["a"]["c"]["USA"] = 59 ;

IS IT POSSIBLE ?

like image 912
Asif Iqbal Avatar asked Jan 13 '15 05:01

Asif Iqbal


1 Answers

This will work.

$input["a"]["b"]["UK"]   = 96 ;    
$input["a"]["c"]["UK"]   = 69 ;
$input["a"]["bp"]["USA"] = 29 ;
$input["a"]["c"]["USA"]  = 59 ;
$input["a"]["dd"]["UK"]  = 31 ;
$input["a"]["cg"]["UK"]  = 38 ;
$tempResult = array();

foreach($input as $key => $value){

    foreach($value as $subkey => $subvalue){

        foreach($subvalue as $finalkey => $finalvalue){

            if($finalkey == 'USA') {

                $tempResult[$key][$subkey][$finalkey] = $finalvalue;
            }
        }
    }
}

echo '<pre>';print_r($tempResult);echo '</pre>';
like image 111
user2944231 Avatar answered Sep 20 '22 09:09

user2944231