0

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
            )
3
  • Try to index both array by uid_apps if possible. Commented Jan 8, 2020 at 5:19
  • you need to match the uid_apps of first array with uid_apps of second array ? Commented Jan 8, 2020 at 5:25
  • you can do it using loop Commented Jan 8, 2020 at 5:27

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.