If you notice blade_server_chassis_row = unknown_chassis_row and blade_server_chassis_col = unknown_chassis_col
in the [1] and [2] array.
Im trying to compare them to alert my if this happens.
Is this right?
but I donât think that is the right way to go about it. Will $assetArray only ever have these two elements, $assetArray[0] and $assetArray[1] ? Or will there be other sets of data? if so, how will they be arranged in the array?
Right, so⌠lets take a step back here, because the title implies a broader picture.
Presumably, you could have many blade servers. Where is this unknown coming from? Have you merged another array? Is this form data youâre handling?
Is the better statement of your problem that you want to compare a single new object against potentially many extant objects in the array? Is the array in a reliable order? Will it always be a given size?
Yes, I need to explain the problem,
I have 5 different types of assetsâŚBlade Servers, Network Cards, Network Modules, Network Standards, and Unknowns.
Any combination of them can belong to a chassis (they would all be given the same chassis_id)
I want tto have some sort of collision detection if they belong to the same row and column in the chassis (chassis_row, chassis_col)I can get all the assets assigned to a chassis into an array,
$assetArray
but dont know how to do the collision detection thing
Ideally, theyâd have had the same property names (âchassis_rowâ instead of âunknown_chassis_rowâ, etc), but i imagine that ship has sailed at this point. I will, for the moment, assume that weâre stuck with the names weâve got. If that ship hasnt sailed yet, iâd advise making those changes.
foreach member of the array;
step 1: Identify the current itemâs row and col. array_filter the array such that the internal function checks the filter itemâs row and col against the foreach itemâs row and col; return true if and only if they both match. Catch the result in a variable.
If the result of the array filter is of length greater than 1, there was a collision; the filtered array holds all of the collisions for that item.
Endforeach.
The problem is up higher. The âArrayâ is coming from incorrect SQL queries and likely DB design issue. Op was not able to figure out how to get me an SQL dump of the DB.
As I understand it, this data array is coming from a number of tables or sources that have similar field names containing the source identifier in the field name. IF you had a structured data array which was built with the chassis_row as the primary array key and chassis_col as the secondary array key you could simply count this array[chassis_row][chassis_col] and you would know you have duplicates. As we have a single simple array we will need to build it.
I will go on the assumption that the field called name will be the first field in this array. This will allow you to grab the array keys and use reset() to get the full name of that first compound name field, e.g.
blade_server_name,
network_cards_name,
unknown_name etc.
You can then use str_replace() to remove name from these compound names to get the unique $identifier of this array. You can then use this with the field you wish to get the value from.
$identifier.'chassis_row'
You can then build the $newdata array placing the chassis_row as the primary key and chassis_col as the secondary array key then count this sub array as you loop through the $newdata and if the count is greater than 1 place this array in a $collision array.