Intersection of one multidimensional arrays in PHP

75 views Asked by At

I have the following array :

$output = array(
  1507073550 => array(
    0 => array(
        "userid" => "1507073550"
        "username" => "ma_alikhani"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/75538099_557824008392923_8054831368279949312_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
    1 => array(
        "userid" => "11863258101"
        "username" => "rasouli680"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/70326284_949495768737898_5241573836020776960_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
    2 => array(
        "userid" => "16528062"
        "username" => "alireza"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/70597112_740563976416368_5253996423334068224_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
  )
  16528062 => array(
    0 => array(
        "userid" => "16528062"
        "username" => "alireza"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/70597112_740563976416368_5253996423334068224_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
    1 => array(
        "userid" => "1507073550"
        "username" => "ma_alikhani"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/75538099_557824008392923_8054831368279949312_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
  )
)

and I want to intersection of keys of this array. get first key "1507073550" and second key "16528062" and intersect all of their data. it's not always have 2 keys, it might have +2 keys, i wrote this code but I'm getting array to string conversion error.

            $keys = array_keys($output);
            foreach ($keys as $index => $values)
            {
                $current_value = $output[$values]; // or $current_value = $a[$keys[$index]];
                $next_key = next($keys);
                $next_value = $output[$next_key] ?? null; // for php version >= 7.0
                $a[] = array_intersect_assoc($current_value,$next_value);
            }

and I'm expecting this result :

array(
    0 => array(
        "userid" => "1507073550"
        "username" => "ma_alikhani"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/75538099_557824008392923_8054831368279949312_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
    1 => array(
        "userid" => "16528062"
        "username" => "alireza"
        "type" => "comment"
        "profile_image" => "https://instagram.fgbb2-1.fna.fbcdn.net/v/t51.2885-19/s150x150/70597112_740563976416368_5253996423334068224_n.jpg?_nc_ht=instagram.fgbb2-1.fna.fbcdn.net&_nc_ohc"
    )
)

I really don't know how to do it ! I'd be appreciated for your helps.

1

There are 1 answers

0
loydg On
function intersect(array $src, array $keys)
{
    // Require that both $src and $keys have data
    if (!$src || !$keys) {
        return [];
    }

    // Hold the users for each key in $keys
    $sets = [];

    // Store the users from $src to $sets as dictated by $keys
    foreach ($keys as $key) {
        if (isset($src[$key])) {
            // Re-key the list of users with their user id
            $userIds = array_column($src[$key], 'userid');
            $sets[$key] = array_combine($userIds, $src[$key]);
        }
    }

    if (count($sets) !== count($keys)) {
        // Up to you if you want to require that all keys must be valid/present in the $src
    }

    // Get the users present in all of the set dictated by $keys
    $users = call_user_func_array('array_intersect_key', $sets);

    return $users;
};

To use:

$output = [ ... ];  // $ouput in the question
$keys = [1507073550, 16528062];  // see question
$users = intersect($output, $keys);