Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.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_Json_Object - Fatal编程技术网

Php 如何合并或搜索对象?

Php 如何合并或搜索对象?,php,json,object,Php,Json,Object,这是我的问题: 我有一个充满数组的对象,看起来像这样 [376339] => Array ( [0] => 1f422730-f54b-4e4d-9289-10258ce74446 [1] => 60dc4646-06ce-44d0-abe9-ee371847f4df ) [376339] => Array ( [0] => Array (

这是我的问题:

我有一个充满数组的对象,看起来像这样

[376339] => Array
    (
        [0] => 1f422730-f54b-4e4d-9289-10258ce74446
        [1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
    )
[376339] => Array
    (
        [0] => Array
            (
                [id] => 1f422730-f54b-4e4d-9289-10258ce74446
                [percentage] => 32
                [destinations] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 59826
                                [destination_id] => 59826
                                [type] => Destination
                                [dequeue] => 
                                [value] => xxxxxxxxxxx
                            )
                    )
            )
        [1] => Array
            (
                [id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
                [percentage] => 68
                [destinations] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 60046
                                [destination_id] => 60046
                                [type] => Destination
                                [dequeue] => 
                                [value] => xxxxxxxxxxxx
                            )

                    )
            )
    )
我需要搜索另一个对象以找到具有匹配ID的对象,如下所示。有没有一种不用付小费的方法?有几个,我不想每次都在整个对象上循环

stdClass Object
(
    [id] => 1f422730-f54b-4e4d-9289-10258ce74446
    [percentage] => 32
    [destinations] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 59826
                    [destination_id] => 59826
                    [type] => Destination
                    [dequeue] => 
                    [value] => xxxxxxxxxxx
                )

        )

)
stdClass Object
(
    [id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
    [percentage] => 68
    [destinations] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 60046
                    [destination_id] => 60046
                    [type] => Destination
                    [dequeue] => 
                    [value] => xxxxxxxxxxxx
                )

        )

)
我需要它最终看起来像这样

[376339] => Array
    (
        [0] => 1f422730-f54b-4e4d-9289-10258ce74446
        [1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
    )
[376339] => Array
    (
        [0] => Array
            (
                [id] => 1f422730-f54b-4e4d-9289-10258ce74446
                [percentage] => 32
                [destinations] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 59826
                                [destination_id] => 59826
                                [type] => Destination
                                [dequeue] => 
                                [value] => xxxxxxxxxxx
                            )
                    )
            )
        [1] => Array
            (
                [id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
                [percentage] => 68
                [destinations] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 60046
                                [destination_id] => 60046
                                [type] => Destination
                                [dequeue] => 
                                [value] => xxxxxxxxxxxx
                            )

                    )
            )
    )
我不确定这是否有意义,这就是为什么我有两个初始输出,我需要以某种方式合并成一个。这一切都来自一个巨大的json对象,我只是使用
json\u decode($jsonstaff)
对其进行解码

如果我在decode函数中添加
true
,这会更容易吗?如果我能像在python中一样搜索它,那就太好了。但事实上,我不知道如何获得所需的输出


注意:无法更改输入json,我与创建它的人没有任何关联。

首先循环输入数组,并创建一个以键为id的数组

$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
    $output[$obj->id] = $obj;
}
然后可以通过搜索数组键上的id来构建另一个数组

$massive_search_array = array(376339 => array
    (
        0 => 1f422730-f54b-4e4d-9289-10258ce74446,
        1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
    )
);

$final_output = array();
foreach($massive_search_array as $index => $searches){
    foreach($searches as $search){
        if(isset($output[$search])){
            $final_output[$index][] = $output[$search];
        }
    }
}

您已经显示了两个独立的对象;你打算怎么翻查它们?它们是另一个数据结构的一部分吗?