简体   繁体   English

PHP - 从对象数组的搜索结果中填充新数组

[英]PHP - Populate new array from search result of array of objects

All,全部,

I have a Widget Config array like this:我有一个像这样的小部件配置数组:

Array 
( 
[0] => Array ( [0] => 1 [1] => apple ) 
[1] => Array ( [0] => 1 [1] => orange ) 
[2] => Array ( [0] => 2 [1] => banana)
)

I have an array of Widget Objects like this:我有一个这样的小部件对象数组:

Array 
( 
[0] => XYZ_Widget Object ( 
                           [position:XYZ_Widget:private] => 1 
                           [widgetId:XYZ_Widget:private] => apple 
                         ) 
[1] => XYZ_Widget Object ( 
                           [position:XYZ_Widget:private] => 2 
                           [widgetId:XYZ_Widget:private] => banana 
                         ) 
[2] => XYZ_Widget Object (  
                           [position:XYZ_Widget:private] => 3 
                           [widgetId:XYZ_Widget:private] => orange
                         ) 
)

For each array Item in Widget Config array, I need to search the array of Widget Objects for widgetId and if it's found, I need to create a new Array of Widget Objects with the found items.对于 Widget Config 数组中的每个数组 Item,我需要在 Widget 对象数组中搜索widgetId ,如果找到,我需要使用找到的项目创建一个新的 Widget 对象数组。

Ex: The new array of Widget Objects created after searching for items in Widget Config array will look like:例如:在 Widget Config 数组中搜索项目后创建的新 Widget 对象数组将如下所示:

Array 
( 
[0] => XYZ_Widget Object ( 
                           [position:XYZ_Widget:private] => 1 
                           [widgetId:XYZ_Widget:private] => apple 
                         ) 
[1] => XYZ_Widget Object ( 
                           [position:XYZ_Widget:private] => 3 
                           [widgetId:XYZ_Widget:private] => orange 
                         ) 
[2] => XYZ_Widget Object (  
                           [position:XYZ_Widget:private] => 2 
                           [widgetId:XYZ_Widget:private] => banana
                         ) 
)

How do I do this through PHP?我如何通过 PHP 做到这一点?

$result = array();
foreach ($configuration as $widgetConf) {
    foreach ($widgets as $widget) {
        if ($widgetConf[1] == $widget->widgetId) {
            $result[] = $widget;
            continue 2;
        }
    }
}

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

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