1

I have an object/array like this:

[LineItems] => Array
                (
                    [0] => stdClass Object
                        (

                            [ProductNumber] => PAC-051-9716
                            [Description] => KIT CLOSURE 6" BUTT THRD BLK
                            [Cost] => 24.84
                            [ExtCost] => 24.84
                            [OrdNum] => X4146223

                        )

                )

And the other object/array looks like this:

                    [0] => VendorBillItem Object
                        (   
                            [vendorName] => PAC-051-9716
                            [quantity] => 1
                            [rate] => 24.84
                            [amount] => 24.84
                        )

How can I check if [ProductNumber] field value from the first array exist in the 2nd array by checking it against [vendorName] field value?

Thanks in advance. Cheers!

1
  • Please consider that both array may have multiple element counts. I just post 1 for each array. Thanks. Commented Jan 31, 2015 at 18:40

2 Answers 2

1

I recommend you build an index for 2nd array.

foreach ($vendorBills as $key => $vendorBill) {
  empty($index[$vendorBill->vendorName]) && $index[$vendorBill->vendorName] = array();
  $index[$vendorBill->vendorName][] = $key;
}

After that just check

!empty($index[$lineItem->ProductNumber])
Sign up to request clarification or add additional context in comments.

Comments

0

Suppose two arrays are named as $lineitemsArray and $venderBillArray

foreach($lineitemsArray as $lineItem)
{
    foreach($venderBillArray as $vendorItem)
    {
        if($lineItem->ProductNumber==$vendorItem->vendorName)
        {

            echo "equal";

        }
        else{
            echo "not equal";
        }
     }
}

1 Comment

Thanks but the 2 arrays can both have multiple elements so I need to loop both arrays. I was looking for a fast and easy way to check if the ProductNumber exist in the 2nd array. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.