I have a Array containing a set of Arrays...
array (size=5)
0 =>
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Naaldhout - gewone den' (length=22)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string 'Middeloud naaldhout (20 - 60j)' (length=30)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
1 =>
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Loofhout - eik of Amerikaanse eik' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
I also have an Array that holds a list of Array keys that should be used to check the main Array (definition above) upon adding a new entry. If these key / values already excist - I don't want to add the item to the main Array. If one or more key / values does not excist ... the array can be added to the main collection.
array (size=7)
'BMS',
'ONT',
'SLG',
'BED',
'NISCODE',
'KLASSE',
'POLNR'
Situation 1 ; adding a Arraythat already excists on all defined keys.
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Loofhout - eik of Amerikaanse eik' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
Situation 2 ; adding a Arraythat does not excists on all defined keys. Note that the BMSkey has a value that the main collection does not know yet.
array (size=7)
'BMS' =>
array (size=2)
'key' => string 'Boomsoort' (length=9)
'value' => string 'Unadded value' (length=33)
'ONT' =>
array (size=2)
'key' => string 'Ontwikkelingsfase' (length=17)
'value' => string '10' (length=2)
'SLG' =>
array (size=2)
'key' => string 'Sluitingsgraad' (length=14)
'value' => string 'Meer dan 2/3' (length=12)
'BED' =>
array (size=2)
'key' => string 'Bedrijfsvorm' (length=12)
'value' => string 'Hooghout' (length=8)
'NISCODE' =>
array (size=2)
'key' => string 'Gemeentecode overeenkomstig het NIS' (length=35)
'value' => string '46021' (length=5)
'KLASSE' =>
array (size=2)
'key' => string 'Bosclassificatie' (length=16)
'value' => string 'Bos' (length=3)
'POLNR' =>
array (size=2)
'key' => string 'Polygoonnummer' (length=14)
'value' => string '32239' (length=5)
I'm looking for the best way of checking the main Array collection if it already contains an Array where the keys needed to be checked are given by an other Array.
I've tried looping the 3 Arrays to check with each other - the code I've created is quite robust and big. I would like to see some fresh tries that can lead me in reducing the lines of code... Pseudo code for what I have
/**
* @Param collection = Global collection
* @Param requirekeystocheck = Array keys to check for excistance
* @Param arraytoadd = Array to be added
* @return Boolean
*/
Function checkExcistArrayOfKeysForArray($collection, $requirekeystocheck, $arraytoadd){
Loop main Array $collection as $collectionItem
Loop Array $requirekeystocheck with keys to check as $uniqueKey
Check if main $collectionItem[$uniqueKey] contains $arraytoadd[$uniqueKey]
Check if the values are equal (already present)
If all values in $requirekeystocheck excist in $collectionItem - the item that needs to be added already excists.
Return TRUE
If not all values excist - the item can be added
Return TRUE
}
Thanks...