Mr. B
Mr. B

Reputation: 2787

How can I filter arrayTwo based on values in arrayOne using PHP?

I'm sorry for not using the correct terminology. I'm very much a 'weekend warrior' when it comes to programming, but trying to get better.

I have two indexed arrays with associative arrays as the values. ArrayOne has a value for [uid_apps] that I want to use as a filter for ArrayTwo so I can create ArrayThree. The thrid array will only include array items from ArrayTwo that match the [uid_apps] value in ArrayOne.

I looked ar array_combine() and array_intersect(), but I didn't see a clear path to success. I also messed around with array_filter(), but wasn't able to make it work.

Below are samples of arrayOne, arrayTwo, and the desired arrayThree. Any help you can offer is greatly appreciated.

ArrayOne

        [0] => Array
            (
                [uid_appMembership] => 3
                [uid_apps] => 1
                [uid_main] => 3
                [privileges] => 555
            )

        [1] => Array
            (
                [uid_appMembership] => 4
                [uid_apps] => 3
                [uid_main] => 3
                [privileges] => 555
            )

ArrayTwo

        [0] => Array
            (
                [uid_apps] => 1
                [name_apps] => GHS Walk Through Evaluation
                [site_apps] => ghs_001
                [team_apps] => ghs_admin
                [admin_uid] => 2
                [dir_apps] => ghs_walk-through-evaluation
            )

        [1] => Array
            (
                [uid_apps] => 2
                [name_apps] => CTE Work Based Learning Solution
                [site_apps] => do_000
                [team_apps] => do_cte
                [admin_uid] => 3
                [dir_apps] => do_cte-wbl
            )

        [2] => Array
            (
                [uid_apps] => 3
                [name_apps] => GHS Parking Permit Solution 
                [site_apps] => ghs_001
                [team_apps] => ghs_parking
                [admin_uid] => 3
                [dir_apps] => ghs_parking-permits
            )

        [3] => Array
            (
                [uid_apps] => 4
                [name_apps] => GHS F-List
                [site_apps] => ghs_001
                [team_apps] => ghs_counseling
                [admin_uid] => 3
                [dir_apps] => ghs_flist
            )

Desired ArrayThree I'd like to use arrayOne and arrayTwo to create this array.

        [0] => Array
            (
                [uid_apps] => 1
                [name_apps] => GHS Walk Through Evaluation
                [site_apps] => ghs_001
                [team_apps] => ghs_admin
                [admin_uid] => 2
                [dir_apps] => ghs_walk-through-evaluation
            )
        [1] => Array
            (
                [uid_apps] => 4
                [name_apps] => GHS F-List
                [site_apps] => ghs_001
                [team_apps] => ghs_counseling
                [admin_uid] => 3
                [dir_apps] => ghs_flist
            )

Upvotes: 0

Views: 59

Answers (2)

Hitesh Tripathi
Hitesh Tripathi

Reputation: 854

loop through array and check uid_apps value is available in other array or not if available than add it in third array.check using in array.

    $arr1 =  array(
            array(   
                'uid_appMembership' => 3,
                'uid_apps'          => 1,
                'uid_main'          => 3,
                'privileges'        => 555
            ),
            array(
                'uid_appMembership' => 4,
                'uid_apps'          => 3,
                'uid_main'          => 3,
                'privileges'        => 555
            )
        );

$arr2 = array(
            array(
                'uid_apps' => 1,
                'name_apps' => 'GHS Walk Through Evaluation',
                'site_apps' => 'ghs_001',
                'team_apps' => 'ghs_admin',
                'admin_uid' => 2,
                'dir_apps' => 'ghs_walk-through-evaluation'
            ),array(
                'uid_apps' => 2,
                'name_apps' => 'CTE Work Based Learning Solution',
                'site_apps' => 'do_000',
                'team_apps' => 'do_cte',
                'admin_uid' => 3,
                'dir_apps' => 'do_cte-wbl'
            ),array(
                'uid_apps' => 3,
                'name_apps' => 'GHS Parking Permit Solution' ,
                'site_apps' => 'ghs_001',
                'team_apps' => 'ghs_parking',
                'admin_uid' => 3,
                'dir_apps' => 'ghs_parking-permits'
            ),array(
                'uid_apps' => 4,
                'name_apps' => 'GHS F-List',
                'site_apps' => 'ghs_001',
                'team_apps' => 'ghs_counseling',
                'admin_uid' => 3,
                'dir_apps' => 'ghs_flist'
            )
        );
    $arr3 = array();
    foreach ($arr2 as $key => $value) {
        $res = chk_val($arr1,$value['uid_apps']);
        if($res == true){
            array_push($arr3,$arr2[$key]); 
        }
    }

    function chk_val($arr,$val){
        foreach ($arr as $key => $value) {
            if(in_array($val,$value)){
               return true;
            }else{
                return false;
            }

        }
    }

Working example : http://phpfiddle.org/main/code/sdri-fbpk

Upvotes: 1

Amit Sharma
Amit Sharma

Reputation: 1795

here is how you can get desired array

$dataa = array();
foreach ($names as $key => $name) {
   foreach($ips as $key2=>$ip){
    if($name['uid_apps'] == $ip['uid_apps']){
        $dataa[] = $name;
    }
  }       
}
print_r($dataa);

Upvotes: 0

Related Questions