1

I am trying to compare two arrays $result_for_vendors and $vendorAcctResults to get matching values and return false when it cannot find a compared value.

Everytime, I run below code, I get first value true and rest all false even though the value matches. I am not sure what am I doing wrong.

$result_for_vendors contains below raw data:

Array ( [0] => stdClass Object ( [id] => 1 [vendorname] => Coke ) [1] => stdClass Object ( [id] => 2 [vendorname] => Pepsi ) [2] => stdClass Object ( [id] => 3 [vendorname] => Dr. Pepper / 7 Up ) [3] => stdClass Object ( [id] => 4 [vendorname] => Frito Lay ) [4] => stdClass Object ( [id] => 5 [vendorname] => Blue Bunny ) [5] => stdClass Object ( [id] => 6 [vendorname] => Yummy ) [6] => stdClass Object ( [id] => 7 [vendorname] => Ork Farm ) [7] => stdClass Object ( [id] => 8 [vendorname] => Borden ) [8] => stdClass Object ( [id] => 9 [vendorname] => Highland ) [9] => stdClass Object ( [id] => 10 [vendorname] => Nesquek ) [10] => stdClass Object ( [id] => 11 [vendorname] => Red Bull ) ) 

And $vendorAcctResults array contains below data:

Array ( [0] => stdClass Object ( [id] => 12 [vendor_id] => 1 [store_id] => 2 [account_no] => 434564 ) [1] => stdClass Object ( [id] => 13 [vendor_id] => 2 [store_id] => 2 [account_no] => 554566873w ) [2] => stdClass Object ( [id] => 14 [vendor_id] => 3 [store_id] => 2 [account_no] => 7786787934 ) [3] => stdClass Object ( [id] => 15 [vendor_id] => 4 [store_id] => 2 [account_no] => 3453434232 ) [4] => stdClass Object ( [id] => 16 [vendor_id] => 5 [store_id] => 2 [account_no] => 3453453456 ) [5] => stdClass Object ( [id] => 17 [vendor_id] => 6 [store_id] => 2 [account_no] => 3w3332432 ) [6] => stdClass Object ( [id] => 18 [vendor_id] => 7 [store_id] => 2 [account_no] => 5656767783 ) [7] => stdClass Object ( [id] => 19 [vendor_id] => 8 [store_id] => 2 [account_no] => 675665436 ) [8] => stdClass Object ( [id] => 20 [vendor_id] => 9 [store_id] => 2 [account_no] => 765756754 ) [9] => stdClass Object ( [id] => 21 [vendor_id] => 10 [store_id] => 2 [account_no] => 657568567 ) [10] => stdClass Object ( [id] => 22 [vendor_id] => 11 [store_id] => 2 [account_no] => 678567456 ) ) 

Here's my PHP:

function vendorKeyCheck ($array, $keyName, $keyCheck) { foreach ($array as $key => $value) { if ($value->$keyName == $keyCheck) { $arrayData = array('vendorid' => $value->vendor_id, 'storeid' => $value->store_id, 'accountno' => $value->account_no ); return $arrayData; } else { $arrayData = false; return $arrayData; } } //return false; } foreach ($result_for_vendors as $key => $vendorAcct) { $acctData = vendorKeyCheck($vendorAcctResults, "vendor_id", $vendorAcct->id); if ($acctData) { print_r($acctData['accountno']); } elseif ($acctData == false) { echo "Not found<br>"; } else { echo "Error<br>"; } } 
4
  • In your else statement it should be return $arrayDataonly. No false. Commented Sep 1, 2015 at 0:56
  • $vendorAcctResults is NULL according to your snippet. Commented Sep 1, 2015 at 0:57
  • Thanks guys for your quick response. I will create some sample data for both array and get back to you guys shortly. Commented Sep 1, 2015 at 1:00
  • Just a comment, in your elseif statement, you just check against === false Commented Sep 1, 2015 at 1:20

1 Answer 1

1

You have the right answer in your own code:

function vendorKeyCheck ($array, $keyName, $keyCheck) { foreach ($array as $key => $value) { if ($value->$keyName == $keyCheck) { $arrayData = array('vendorid' => $value->vendor_id, 'storeid' => $value->store_id, 'accountno' => $value->account_no ); return $arrayData; } else { $arrayData = false; return $arrayData; } } //return false; } 

You should remove else statement completly and uncomment //return false;
Otherwise your code only checks first row in the array.

Result should be like this:

function vendorKeyCheck ($array, $keyName, $keyCheck) { foreach ($array as $key => $value) { if ($value->$keyName == $keyCheck) { $arrayData = array('vendorid' => $value->vendor_id, 'storeid' => $value->store_id, 'accountno' => $value->account_no ); return $arrayData; } } return false; } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Nagh. Your suggestion works. I am confused why else statement is not needed when using if else login?
Because you doing check in the loop. And you only need to return when all rows are processed or when match has been found. Otherwise you check just one - first row and then return no matter what.
understood. I will keep this in mind in all future coding. :)
@JoaquínO okay, however I don't think that it is really neccessary in this very basic code.
@Ray You should see it as, if you return anything in the function it will break there and stop all other actions within that function. There are exceptions but thats what it comes down to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.