I have a dynamic PHP generated array ($mapHitArriving) based on arriving guests which results like the following:
[0] => Array
(
[id] => 1098
[source] => 31
[name] => Steve test1
[date] => 2020-12-19
[dateLast] => 2020-12-20
[cost] => 250
[unitno] => 1
[utitle] => Queen Plus Room
)
[1] => Array
(
[id] => 1099
[source] => 31
[name] => Steve Test2
[date] => 2020-12-19
[dateLast] => 2020-12-20
[cost] => 250
[unitno] => 3
[utitle] => Queen Plus Room
)
I'm trying to assign variables IF [unitno] is present. The variable is based on the unit number and has a value of the unit number and a concantonated 'a' since we're targeting arriving guests. Research has me looking at using array_search and targeting the index column of "unitno".
Using the following only produces 1 result. Only the variable for unit3 is created.
if(array_search(1,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit1 = '1a'; };
if(array_search(2,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit2 = '2a'; };
if(array_search(3,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit3 = '3a'; };
if(array_search(4,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit4 = '4a'; };
if(array_search(5,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit5 = '5a'; };
if(array_search(6,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit6 = '6a'; };
if(array_search(7,array_column($mapHitArriving, 'unitno'))== TRUE) { $unit7 = '7a'; };
Why does only the second instance of [unitno] create it's result? Being that they are independent array_search conditions I'm expecting a result for both unit1 and unit3.
Should I be searching and assigning differently as a best practice?
EDIT: adding a reference to Identical vs Equal operators related to the problem encountered.
EDIT 2: The original logic seems to fail when introducing the comparison to other arrays. Initially we look to see if guests are arriving (Array: $mapHitArriving - result: unit id concantonated with 'a'). Then we look to see who's Departing (Array: $mapHitDeparting - result: '1d') and lastly who is neither Arriving or Departing which is Staying (Array: $mapHitStaying - result: no concantonated letter just the unit id). It seems the first conditional is evaluating as TRUE so the others fail to execute. This condition was not present when there was some result of the initial array so I believe I need declare the array even if empty(?):
if(array_search(1,array_column($mapHitArriving, 'unitno'))!== FALSE) { $unit1 = '1a'; }
elseif (array_search(1,array_column($mapHitDeparting,'unitno'))!== FALSE) { $unit1 = '1d'; }
elseif (array_search(1,array_column($mapHitStaying,'unitno'))!== FALSE) { $unit1 = '1'; }