Reputation: 2198
I'm encountering the following problem: I have an object containg several information about an object, which comes from a PDO-fetchAll().
$sSql = "SELECT * FROM my_table";
$result = $this->db->fetchAll($sSql);
The output looks like this:
array(7) {
[0]=>
object(stdClass)#8 (13) {
["id"]=> string(6) "393605"
["profile_id"]=> string(2) "38"
["sku"]=> string(11) "01-SSC-4314"
[1]=>
object(stdClass)#7 (13) {
["id"]=> string(6) "393606"
["profile_id"]=> string(2) "38"
["sku"]=> string(11) "01-SSC-4315"
}
Everything fine so far. Now I get another input-array. The array looks like this.
array(7) {
[0]=>
array(19) {
[0]=> string(11) "01-SSC-4314"
[1]=> string(10) "3406190219"
[2]=> string(29) "24X7 SUPPORT FOR NSA 2600 1YR"
}
[1]=>
array(19) {
[0]=> string(11) "01-SSC-4316"
[1]=> string(10) "3406190219"
[2]=> string(29) "24X7 SUPPORT FOR NSA 2601 1YR"
}
[2]=>
array(19) {
[0]=> string(11) "01-SSC-4317"
[1]=> string(10) "3406190219"
[2]=> string(29) "24X7 SUPPORT FOR NSA 2600 3YR"
}
}
What I want: I want the difference between those two given elements. I was thinking about using array_diff, but everytime I tried to cast my object to an array, it ended up still beeing an object.
(For reference: I tried $aArray = (array)$result; (which still was an object afterwards) PHP 5.3
to give an example, what I need exactly:
In my object, there are two different entries. Lets focus on the sku
for this case. There is a *4314 and a *4315.
In my input array, there are 3 Elements. As you may see, the *4315-entry is missing. This is exactly what I need now.
So, I need to substract the set of a given input-array from a set of already existing data.
input - object = difference
I could also iterate through the object and check, if a value exists in an array, but I think that isn't suitable for larger objects / arrays. Also, from what I've tried:
$aMissing = array();
// now lets see what what items we have now
foreach($result as $res)
{
if(!in_array($res->sku, $lines)) {
$aMissing = "";
}
}
I wouldn't have the reference, what to add to $aMissing
.
The output should be something like this:
missing items: 01-SSC-4315, (...)
Recommendations? Ideas?
Upvotes: 0
Views: 59
Reputation: 4513
Requirements: Find missing 'master' SKU entries from a source list of items.
Given:
$sourceSkuList
). Where each entry is an stdClass
object.$masterSkuList
) where each entry is an array. I assume the first entry is the SKU.Output:
Method:
The lists are not keyed by SKU and as far as I know are not sorted by SKU.
The most efficient way of doing this would, in the worse case, involve one sequential pass down each list. This would require each list to be sorted in SKU order.
Can we come close to this? yes...
build an array, using $masterSkuList
, that is keyed on SKU. This will be very quick to check as it is a key lookup. This can be created in one sequential pass.
Check the other array (sourceSkuList
) sequentially using the SKU as the key. If it is not found add it to the missing list.
Note: I use extra variables so it is easier to understand.
// create a master sku list keyed by SKU for fast checking later...
$skuLookupList = array();
// fill it... assume key Zero is always the SKU entry
foreach ($masterSkuList as $position => $sku) {
$skuLookupList[$sku[0]] = $position;
}
// output in here...
$missingSkuList = array();
// check each entry...
foreach ($sourceSkuList as $entry) {
$curSku = $entry->sku;
// check source sku exists in the lookup...
if (!isset($skuLookupList[$curSku])) { // add it to the missing list
$missingSkuList[$curSku] = (array) $entry;
}
}
Array
(
[01-SSC-4315] => Array
(
[id] => 393606
[profile_id] => 38
[sku] => 01-SSC-4315
)
)
Test Data:
--- Source (objects) - $sourceSkuList ------
Array(
[0] => stdClass Object
(
[id] => 393605
[profile_id] => 38
[sku] => 01-SSC-4314
)
[1] => stdClass Object
(
[id] => 393606
[profile_id] => 38
[sku] => 01-SSC-4315
)
)
--- Master (array) - $masterSkuList ------
Array(
[0] => Array
(
[0] => 01-SSC-4314
[1] => 3406190219
[2] => 24X7 SUPPORT FOR NSA 2600 1YR
)
[1] => Array
(
[0] => 01-SSC-4316
[1] => 3406190219
[2] => 24X7 SUPPORT FOR NSA 2601 1YR
)
[2] => Array
(
[0] => 01-SSC-4317
[1] => 3406190219
[2] => 24X7 SUPPORT FOR NSA 2600 3YR
)
)
Upvotes: 1