Comparing values in an array?

I have

Array
(
    [0] => Array
        (
            [blade_server_name] => 
            [blade_server_id] => 9
            [blade_server_beginning_x] => 0.0
            [blade_server_beginning_y] => 85.0
            [blade_server_chassis_row] => 7
            [blade_server_chassis_col] => 1
            [blade_server_width] => 7.591
            [blade_server_height] => 1.098
            [blade_server_manufacturer] => Brocade
            [blade_server_model] => VDX 2730
        )

    [1] => Array
        (
            [unknown_name] => 
            [unknown_id] => 5
            [unknown_beginning_x] => 0.0
            [unknown_chassis_row] => 7
            [unknown_chassis_col] => 1
            [unknown_width] => 4.000
            [unknown_height] => 4.000
            [unknown_manufacturer] => Unknown
            [unknown_model] => Unknown
        )

)

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?

 for ($i = 0; $i < sizeof($array);$i++){
  if($array[$i][4] == $array[$i + 1][4] & $array[$i][4] == $array[$i + 1][5])
  {
  //alert
  }
}

Does what you’ve written work?

A neater solution might be to use a foreach loop instead.

The & should be &&

would something like this work?

$keys = array_keys($assetArray);
for($i = 0; $i < count($assetArray); $i++) {
    echo $keys[$i] . "{<br>";
    foreach($assetArray[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
		
    }
    echo "}<br>";
}

the result…

0{
blade_server_name :
blade_server_id : 9
blade_server_beginning_x : 0.0
blade_server_beginning_y : 85.0
blade_server_chassis_row : 7
blade_server_chassis_col : 1
blade_server_width : 7.591
blade_server_height : 1.098
blade_server_manufacturer : Brocade
blade_server_model : VDX 2730
}
1{
unknown_name :
unknown_id : 5
unknown_beginning_x : 0.0
unknown_chassis_row : 7
unknown_chassis_col : 1
unknown_width : 4.000
unknown_height : 4.000
unknown_manufacturer : Unknown
unknown_model : Unknown
}

then, in the foreach loop, should I do

if(($key['blade_server_row'] == ($key + 1)['unknown_chassis_row']) && ($key['blade_server_col'] == ($key + 1)['unknown_chassis_col']))

to test the equality? cause I’m getting

Warning: Illegal string offset ‘blade_server_row’ in C:\xampp\htdocs\DCT\1\chassises\show_chassis.php on line 374

I think what you are aiming for is:

if($assetArray[$key]['blade_server_row'] == $assetArray[$key + 1]['unknown_chassis_row'] && $assetArray[$key]['blade_server_col'] == $assetArray[$key + 1]['unknown_chassis_col']){
//do something
}

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?

1 Like

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?

1 Like

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.

2 Likes

Yes, you are right here but you are missing the chassis from the name of your keys.

1 Like

I can rename the keys of each array (I didn’t know I can do that) so that’ll make it better. Thanks…

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.

Really it is all pretty simple.

if(!empty($data)):
	$newdata = array();
	foreach($data as $k => $arr): 
		$keys = array_keys($arr);
		$identifier = str_replace('name','',reset($keys));
		$newdata[$data[$k][$identifier.'chassis_row']][$data[$k][$identifier.'chassis_col']][] = $arr;
	endforeach;	
		  
	$collision = array();
	foreach($newdata as $k => $arr):
		foreach($newdata[$k] as $k2 => $arr2):	
			if(count($newdata[$k][$k2]) > 1):
				$collision[$k] = $arr2;
			endif;	
		endforeach;
	endforeach;
	
	echo "<pre>";
	print_r($collision);	
	echo "</pre>";
endif;

This would result in an array of conflicting data like this depending on the data presented.

Array
(
    [7] => Array
        (
            [0] => Array
                (
                    [blade_server_name] => 
                    [blade_server_id] => 9
                    [blade_server_beginning_x] => 0.0
                    [blade_server_beginning_y] => 85.0
                    [blade_server_chassis_row] => 7
                    [blade_server_chassis_col] => 1
                    [blade_server_width] => 7.591
                    [blade_server_height] => 1.098
                    [blade_server_manufacturer] => Brocade
                    [blade_server_model] => VDX 2730
                )

            [1] => Array
                (
                    [unknown_name] => 
                    [unknown_id] => 8
                    [unknown_beginning_x] => 0.0
                    [unknown_chassis_row] => 7
                    [unknown_chassis_col] => 1
                    [unknown_width] => 4.000
                    [unknown_height] => 4.000
                    [unknown_manufacturer] => Unknown
                    [unknown_model] => Unknown
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [blade_server_name] => 
                    [blade_server_id] => 7
                    [blade_server_beginning_x] => 0.0
                    [blade_server_chassis_row] => 5
                    [blade_server_chassis_col] => 1
                    [blade_server_width] => 4.000
                    [blade_server_height] => 4.000
                    [blade_server_manufacturer] => Unknown
                    [blade_server_model] => Unknown
                )

            [1] => Array
                (
                    [unknown_name] => 
                    [unknown_id] => 6
                    [unknown_beginning_x] => 0.0
                    [unknown_chassis_row] => 5
                    [unknown_chassis_col] => 1
                    [unknown_width] => 4.000
                    [unknown_height] => 4.000
                    [unknown_manufacturer] => Unknown
                    [unknown_model] => Unknown
                )

            [2] => Array
                (
                    [network_standards_name] => 
                    [network_standards_id] => 5
                    [network_standards_beginning_x] => 0.0
                    [network_standards_chassis_row] => 5
                    [network_standards_chassis_col] => 1
                    [network_standards_width] => 4.000
                    [network_standards_height] => 4.000
                    [network_standards_manufacturer] => Unknown
                    [network_standards_model] => Unknown
                )

        )

)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.