Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较三个数组中的值并在php中获取特定值_Php_Arrays - Fatal编程技术网

比较三个数组中的值并在php中获取特定值

比较三个数组中的值并在php中获取特定值,php,arrays,Php,Arrays,我比较数组中的三个值,得到所有值 如何输出特定值或只想输出的值 因为我想输出我只需要的值 我有以下代码: <?php $participants = [ [ 'calleridnum' => 1, 'callee' => 'yay' ], [ 'calleridnum' => 2, 'callee' => 'yay' ], [ 'calleridnum' => 3,

我比较数组中的三个值,得到所有值

如何输出特定值或只想输出的值

因为我想输出我只需要的值

我有以下代码:

<?php
$participants = [
    [   'calleridnum' => 1,
        'callee' => 'yay' 
    ],
    [   'calleridnum' => 2,
        'callee' => 'yay' 
    ],
     [  'calleridnum' => 3,
        'callee' => 'yay' 
    ]
];
$conferance_participants = [
    [   'uid' => 1,
        'caller' => 'yay2',
        'dit' => 'deze'
    ],
    [   'uid' => 2,
        'caller' => 'test',
        'dit' => 'wew'
    ]
];
$contacts = [
    [   'name' => 1,
        'test' => 'yay2',
        'limit' => 1
    ],
    [   'name' => 2,
        'test' => 'yay2',
        'limit' => 1
    ]
];



    foreach ($participants as $participant=>$p) {
        foreach ($conferance_participants as $conferance_participant=>$cp) {
            foreach ($contacts as $contact=>$cs) {

                if (($p['calleridnum'] == $cp['uid']) && ($cp['uid'] == $cs['name'])){
                     $conferance_participants[$conferance_participant] = array_merge(
                     $participants[$participant],
                     $conferance_participants[$conferance_participant],
                     $contacts[$contact]
                );

                }
            } 

        } 

    }

echo "<pre>";
print_r($conferance_participants);
echo "</pre>";

?>
我想尽量减少我的产量

我想从$contacts数组中删除
name
test

我还想从$Conference\u参与者数组中删除调用方
dit

因此,我的输出将是:

<?php

    $participants = [
        [   'calleridnum' => 1,
            'callee' => 'yay'
        ],
        [   'calleridnum' => 2,
            'callee' => 'yay'
        ],
         [  'calleridnum' => 3,
            'callee' => 'yay'
        ]
    ];
    $conferance_participants = [
        [   'uid' => 1,
            'caller' => 'yay2',
            'dit' => 'deze'
        ],
        [   'uid' => 2,
            'caller' => 'test',
            'dit' => 'wew'
        ]
    ];
    $contacts = [
        [   'name' => 1,
            'test' => 'yay2',
            'limit' => 1
        ],
        [   'name' => 2,
            'test' => 'yay2',
            'limit' => 1
        ]
    ];

        foreach ($participants as $participant=>$p) {
            foreach ($conferance_participants as $conferance_participant=>$cp) {
                foreach ($contacts as $contact=>$cs) {

                    if (($p['calleridnum'] == $cp['uid']) && ($cp['uid'] == $cs['name'])){

                         unset($contacts[$contact]['name'], $contacts[$contact]['test']);
                         unset($conferance_participants[$conferance_participant]['caller'], $conferance_participants[$conferance_participant]['dit']);

                         $conferance_participants[$conferance_participant] = array_merge(
                         $participants[$participant],
                         $conferance_participants[$conferance_participant],
                         $contacts[$contact]
                        );

                         $contacts[$contact]['name'] = $cs['name'];

                    }
                }

            }

        }

    echo "<pre>";

    print_r($conferance_participants);

    echo "</pre>";

您可以在合并数组之前取消设置数组键。然后为上面循环所需的$contacts数组再次设置“name”键

以下是修改后的代码示例:

Array
(
    [0] => Array
        (
            [calleridnum] => 1
            [callee] => yay
            [uid] => 1
            [limit] => 1
        )

    [1] => Array
        (
            [calleridnum] => 2
            [callee] => yay
            [uid] => 2
            [limit] => 1
        )

)

我希望这能回答你的问题。谢谢。

合并后,您可以使用
array\u intersect\u key()


你的代码很难理解

您的
foreach
将执行的次数

此代码的
foreach
执行次数将等于或小于您的代码,因为它将在找到匹配项后立即停止

此外,我还创建了一个新函数,用于在另一个数组中搜索,因此它将使下一个处理此代码的人不会在办公桌上碰头

$conference\u参与者
变量的值作为参考,注意foreach声明中的
&
,因此无需担心数组的键


这将使
$conference\u参与者完全符合您的要求。

我从未见过这样声明的数组before@ScottMcGready这是5.4中新增的一种速记方式(我想这是版本号)。@chris85它绝对可怕,而且容易出现问题和混淆。当然是个人意见,根本不是对OP的攻击。你以后会使用单独的数组吗?如果你去掉标识符,就很难识别了。也许只是做一个数组?
<?php

    $participants = [
        [   'calleridnum' => 1,
            'callee' => 'yay'
        ],
        [   'calleridnum' => 2,
            'callee' => 'yay'
        ],
         [  'calleridnum' => 3,
            'callee' => 'yay'
        ]
    ];
    $conferance_participants = [
        [   'uid' => 1,
            'caller' => 'yay2',
            'dit' => 'deze'
        ],
        [   'uid' => 2,
            'caller' => 'test',
            'dit' => 'wew'
        ]
    ];
    $contacts = [
        [   'name' => 1,
            'test' => 'yay2',
            'limit' => 1
        ],
        [   'name' => 2,
            'test' => 'yay2',
            'limit' => 1
        ]
    ];

        foreach ($participants as $participant=>$p) {
            foreach ($conferance_participants as $conferance_participant=>$cp) {
                foreach ($contacts as $contact=>$cs) {

                    if (($p['calleridnum'] == $cp['uid']) && ($cp['uid'] == $cs['name'])){

                         unset($contacts[$contact]['name'], $contacts[$contact]['test']);
                         unset($conferance_participants[$conferance_participant]['caller'], $conferance_participants[$conferance_participant]['dit']);

                         $conferance_participants[$conferance_participant] = array_merge(
                         $participants[$participant],
                         $conferance_participants[$conferance_participant],
                         $contacts[$contact]
                        );

                         $contacts[$contact]['name'] = $cs['name'];

                    }
                }

            }

        }

    echo "<pre>";

    print_r($conferance_participants);

    echo "</pre>";
Array
(
    [0] => Array
        (
            [calleridnum] => 1
            [callee] => yay
            [uid] => 1
            [limit] => 1
        )

    [1] => Array
        (
            [calleridnum] => 2
            [callee] => yay
            [uid] => 2
            [limit] => 1
        )

)
$keys = array_flip(['calleridnum', 'callee', 'uid', 'limit']);

$conferance_participants[$conferance_participant] =
    array_intersect_key(
        array_merge(
            $participants[$participant],
            $conferance_participants[$conferance_participant],
            $contacts[$contact]
        ),
        $keys
    );
count($participants) * count($conferance_participants) * count($contacts);
foreach($conferance_participants as &$record) {
    # find keys of corresponding array matches
    $key_in_participants = _custom_search($record['uid'], $participants, 'calleridnum');
    $key_in_contacts = _custom_search($record['uid'], $contacts, 'name');

    # unset unwanted things
    unset($record['caller'], $record['dit']);

    # activate this code if you want to make false for unmatched records
    /* ***********************************************************************
    if($key_in_participants === false || $key_in_contacts === false) {
        $record['calleridnum'] = $record['callee'] = $record['limit'] = false;
        continue;
    }
    *********************************************************************** */ 

    # setting required things
    $record['calleridnum'] = $participants[$key_in_participants]['calleridnum'];
    $record['callee'] = $participants[$key_in_participants]['callee'];
    $record['limit'] = $contacts[$key_in_contacts]['limit'];
}

function _custom_search($id, $array, $key_to_search) {
   foreach ($array as $key => $val) if($val[$key_to_search] === $id) return $key;
   return false;
}