繁体   English   中英

PHP 中一个多维 arrays 的交集

[英]Intersection of one multidimensional arrays in PHP

我有以下数组:

$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"
    )
  )
)

我想交叉这个数组的键。 获取第一个键“1507073550”和第二个键“16528062”并与它们的所有数据相交。 它并不总是有 2 个键,它可能有 +2 个键,我写了这段代码,但我得到了数组到字符串的转换错误。

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

我期待这个结果:

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

我真的不知道该怎么做。 我将不胜感激您的帮助。

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

要使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM