I have searched and found nothing on this.
I would like some advice or pointers on how I could search a multi dimensional array and update if a value exists or insert if it doesn’t exist.
Eg. at the moment I create an array with these values like this:
Array
(
[0] => Array
(
[quantity] => 1
[supplier_paypal] => paypalaccount1@paypal.com
[supplier_price] => 10
)
[1] => Array
(
[quantity] => 2
[supplier_paypal] => paypalaccount2@paypal.com
[supplier_price] => 20
)
)
Now this is great but it just loops though and can create duplicate email addresses in the array. I need something that I can put in the loop that searches to see if the email exists and if it does then just merely adds the supplier prices together.
Any help or ideas?
Heres what I have tried:
$arrIt = new RecursiveIteratorIterator(
new RecursiveArrayIterator($this->data['payrecipient_data']));
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['supplier_paypal'] === $supplier_info['supplier_paypal']) {
$this->data['payrecipient_dup'][] = iterator_to_array($subArray);
} else {
$this->data['payrecipient_nondup'][] = iterator_to_array($subArray);
}
}
This just enabled me to search through and seperate the arrays into groups of duplicated and none duplicated.
But I don’t know where to start with updating an array so I got lost and stuck.
There are more elegant ways to index the data to find it quicker without looping through the whole thing every time, but this is essentially the basic algorithm you’re looking for.