如何通过id从对象数组中获取对象?

时间:2021-11-20 16:49:19

Assuming that I have an array of objects like this:

假设我有一个像这样的对象数组:

Array
(
    [0] => stdClass Object
        (
            [id] => 10-423-1176
            [qty] => 2
            [price] => 12.6
        )

    [1] => stdClass Object
        (
            [id] => 26-295-1006
            [qty] => 24
            [price] => 230.35
        )

    [2] => stdClass Object
        (
            [id] => 12-330-1000
            [qty] => 2
            [price] => 230.35
        )

And I have another array of object hat looks like this:

我有另一个对象帽子阵列看起来像这样:

Array
(
    [0] => Item Object
        (
            [internalId] => 14062
            [itemVendorCode] => 89-605-1250
        )

    [1] => Item Object
        (
            [internalId] => 33806
            [itemVendorCode] => 89-575-2354
        )

    [2] => Item Object
        (
            [internalId] => 64126
            [itemVendorCode] => 26-295-1006
        )
)

I want to loop through the 2nd array of objects and get the 'itemVendorCode' and then use it as the 'id' to get the object from the first array of objects. Is there a way to obtain what I want without looping the first array? Looping is very costly in my use-case.

我想循环遍历第二个对象数组并获取'itemVendorCode',然后将其用作'id'以从第一个对象数组中获取对象。有没有办法在不循环第一个数组的情况下获得我想要的东西?在我的用例中循环非常昂贵。

Thanks in advance.

提前致谢。

6 个解决方案

#1


2  

You will have to use loops in any case, even if those loops are hidden within PHP built-in functions.

在任何情况下都必须使用循环,即使这些循环隐藏在PHP内置函数中也是如此。

For instance:

$codes = array_map(function ($item) { return $item->itemVendorCode; }, $array2);
$items = array_filter($array1, function ($item) use ($codes) { return in_array($item->id, $codes); });
// $items contains only elements from $array1 that match on $array2

If this will be more efficient than using regular loops is hard to tell.

如果这比使用常规循环更有效,很难说。

Since you are aparently trying to code what is supposed to be a DBMS's job, I recommend you export those tables to a database server such as MySQL instead and let it work its magic on those "JOINs".

由于您正在尝试编写应该是DBMS的工作,我建议您将这些表导出到MySQL等数据库服务器,并让它在这些“JOIN”上发挥作用。

Answering your comment, you could merge with something like this:

回答你的评论,你可以合并这样的东西:

$result = array();
foreach ($array1 as $item1)
    foreach ($array2 as $item2)
        if ($item1->id == $item2->itemVendorCode)
            $result[] = (object)array_merge((array)$item1, (array)$item2));

$result will contain a new set of objects that merge properties from both $array1 and $array2 where they intersect in id == itemVendorCode.

$ result将包含一组新对象,这些对象合并来自$ array1和$ array2的属性,它们在id == itemVendorCode中相交。

#2


2  

Do you need first arrays index keys? if not you could iterate throuh first array once and set key to id. Something like:

你需要第一个数组索引键吗?如果不是,你可以通过第一个数组迭代一次并将键设置为id。就像是:

foreach ($items as $key => $item) {
    $items[$item->id] = $item;
    unset($items[$key]);
}

#3


1  

try using array_search: http://php.net/manual/en/function.array-search.php

尝试使用array_search:http://php.net/manual/en/function.array-search.php

foreach($array2 as $key=>$item) {
    $firstArrayObjectKey = array_search($item['itemVendorCode'], $array1);
    //... do something with the key $firstArrayObjectKey
}

#4


1  

Here is another direct approach to solve this problem, even better than the one I proposed earlier:

这是解决这个问题的另一种直接方法,甚至比我之前提出的方法更好:

// you got the $itemVendorCode from looping through the second array, let say :

$itemVendorCode = "89-605-1250";


// I'm assuming that you converted the array of objects in into accessible multidimensional array

// so the $first_array would look like :

$first_array= array (
        array (
                "id" => "10-423-1176",
                "qty" => 2,
                "price" => 12.6 
        ),
        array (
                "id" => "10-423-1176",
                "qty" => 5,
                "price" => 25 
        ),
        array (
                "id" => "89-605-1250",
                "qty" => 12,
                "price" => 30 
        ) 
);

// Now you can filter the first array using 

$filter = function ($player) use($itemVendorCode) {
    return $player ['id'] == $itemVendorCode;
};

$filtered = array_filter ( $first_array, $filter );


// print the price of the matching filtered item 

print $filtered[key($filtered)]['price'] ;

#5


1  

You can use the array_map and array_filter() function to achieve that.

您可以使用array_map和array_filter()函数来实现它。

Try with this code:

试试这段代码:

<?php

$first = array();
$first[0] = new stdClass;
$first[0]->id = '89-605-1250';
$first[0]->qty = 2;
$first[0]->price = 12.6;

$first[1] = new stdClass;
$first[1]->id = '89-575-2354';
$first[1]->qty = 24;
$first[1]->price = 230.35;


$last = array();
$last[0] = new stdClass;
$last[0]->internalId = 14062;
$last[0]->itemVendorCode = '89-605-1250';

$last[1] = new stdClass;
$last[1]->internalId = 33806;
$last[1]->itemVendorCode = '89-575-2354';

$ids = array_map(function($element){return $element->itemVendorCode;}, $last);

$to_find = $ids[0];

$object = array_filter($first, function($element){global $to_find; return $element->id == $to_find ? true: false;})[0];

print_r($object);

?>

Output:

stdClass Object
(
    [id] => 89-605-1250
    [qty] => 2
    [price] => 12.6
)

#6


0  

In this case you'll need to loop through the first array to get the itemVendorCode.

在这种情况下,您需要遍历第一个数组以获取itemVendorCode。

Right after that you can use the itemValue you got from the previous process to search in a reduced array of the first object using array_reduce function:

在此之后,您可以使用从上一个过程获得的itemValue,使用array_reduce函数搜索第一个对象的简化数组:

http://php.net/manual/en/function.array-reduce.php

#1


2  

You will have to use loops in any case, even if those loops are hidden within PHP built-in functions.

在任何情况下都必须使用循环,即使这些循环隐藏在PHP内置函数中也是如此。

For instance:

$codes = array_map(function ($item) { return $item->itemVendorCode; }, $array2);
$items = array_filter($array1, function ($item) use ($codes) { return in_array($item->id, $codes); });
// $items contains only elements from $array1 that match on $array2

If this will be more efficient than using regular loops is hard to tell.

如果这比使用常规循环更有效,很难说。

Since you are aparently trying to code what is supposed to be a DBMS's job, I recommend you export those tables to a database server such as MySQL instead and let it work its magic on those "JOINs".

由于您正在尝试编写应该是DBMS的工作,我建议您将这些表导出到MySQL等数据库服务器,并让它在这些“JOIN”上发挥作用。

Answering your comment, you could merge with something like this:

回答你的评论,你可以合并这样的东西:

$result = array();
foreach ($array1 as $item1)
    foreach ($array2 as $item2)
        if ($item1->id == $item2->itemVendorCode)
            $result[] = (object)array_merge((array)$item1, (array)$item2));

$result will contain a new set of objects that merge properties from both $array1 and $array2 where they intersect in id == itemVendorCode.

$ result将包含一组新对象,这些对象合并来自$ array1和$ array2的属性,它们在id == itemVendorCode中相交。

#2


2  

Do you need first arrays index keys? if not you could iterate throuh first array once and set key to id. Something like:

你需要第一个数组索引键吗?如果不是,你可以通过第一个数组迭代一次并将键设置为id。就像是:

foreach ($items as $key => $item) {
    $items[$item->id] = $item;
    unset($items[$key]);
}

#3


1  

try using array_search: http://php.net/manual/en/function.array-search.php

尝试使用array_search:http://php.net/manual/en/function.array-search.php

foreach($array2 as $key=>$item) {
    $firstArrayObjectKey = array_search($item['itemVendorCode'], $array1);
    //... do something with the key $firstArrayObjectKey
}

#4


1  

Here is another direct approach to solve this problem, even better than the one I proposed earlier:

这是解决这个问题的另一种直接方法,甚至比我之前提出的方法更好:

// you got the $itemVendorCode from looping through the second array, let say :

$itemVendorCode = "89-605-1250";


// I'm assuming that you converted the array of objects in into accessible multidimensional array

// so the $first_array would look like :

$first_array= array (
        array (
                "id" => "10-423-1176",
                "qty" => 2,
                "price" => 12.6 
        ),
        array (
                "id" => "10-423-1176",
                "qty" => 5,
                "price" => 25 
        ),
        array (
                "id" => "89-605-1250",
                "qty" => 12,
                "price" => 30 
        ) 
);

// Now you can filter the first array using 

$filter = function ($player) use($itemVendorCode) {
    return $player ['id'] == $itemVendorCode;
};

$filtered = array_filter ( $first_array, $filter );


// print the price of the matching filtered item 

print $filtered[key($filtered)]['price'] ;

#5


1  

You can use the array_map and array_filter() function to achieve that.

您可以使用array_map和array_filter()函数来实现它。

Try with this code:

试试这段代码:

<?php

$first = array();
$first[0] = new stdClass;
$first[0]->id = '89-605-1250';
$first[0]->qty = 2;
$first[0]->price = 12.6;

$first[1] = new stdClass;
$first[1]->id = '89-575-2354';
$first[1]->qty = 24;
$first[1]->price = 230.35;


$last = array();
$last[0] = new stdClass;
$last[0]->internalId = 14062;
$last[0]->itemVendorCode = '89-605-1250';

$last[1] = new stdClass;
$last[1]->internalId = 33806;
$last[1]->itemVendorCode = '89-575-2354';

$ids = array_map(function($element){return $element->itemVendorCode;}, $last);

$to_find = $ids[0];

$object = array_filter($first, function($element){global $to_find; return $element->id == $to_find ? true: false;})[0];

print_r($object);

?>

Output:

stdClass Object
(
    [id] => 89-605-1250
    [qty] => 2
    [price] => 12.6
)

#6


0  

In this case you'll need to loop through the first array to get the itemVendorCode.

在这种情况下,您需要遍历第一个数组以获取itemVendorCode。

Right after that you can use the itemValue you got from the previous process to search in a reduced array of the first object using array_reduce function:

在此之后,您可以使用从上一个过程获得的itemValue,使用array_reduce函数搜索第一个对象的简化数组:

http://php.net/manual/en/function.array-reduce.php