【问题标题】:Intersection of one multidimensional arrays in PHPPHP中一个多维数组的交集
【发布时间】:2021-03-14 12:40:53
【问题描述】:

我有以下数组:

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

我真的不知道该怎么做! 非常感谢您的帮助。

【问题讨论】:

  • 听起来您不希望从一个实际的交叉点开始吗?看起来您想从每个子元素中获取第一项,其中 userid 值与顶级数组键匹配?
  • 不是顶级数组键,我想与顶级数组键的子数组相交,如您所见,两个顶级数组键中都有 2 个数组。

标签: php multidimensional-array intersection array-intersect


【解决方案1】:
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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多