Checking values in an array()

I have an array

echo '<pre>'; print_r($assetArray); echo '</pre>';

result



Array
(
    [0] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 5
            [chassis_row] => 1
            [chassis_col] => 1
            [manufacturer] => 
            [model] => 
        )

    [1] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 1
            [chassis_row] => 1
            [chassis_col] => 1
            [manufacturer] => Brocade
            [model] => VDX 2730
        )

    [2] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 3
            [chassis_row] => 2
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3C16791C
        )

    [3] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 2
            [chassis_row] => 3
            [chassis_col] => 1
            [manufacturer] => Aculab
            [model] => E1/T1 PCI
        )

    [4] => Array
        (
            [name] => 
            [id] => 1
            [asset_type] => 4
            [chassis_row] => 4
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3CB9LF10MC
        )

    [5] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 4
            [chassis_row] => 5
            [chassis_col] => 1
            [manufacturer] => Xsigo Systems
            [model] => 8GB Fibre Module
        )

    [6] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 1
            [chassis_row] => 6
            [chassis_col] => 1
            [manufacturer] => Brocade
            [model] => VDX 2730
        )

    [7] => Array
        (
            [name] => 
            [id] => 3
            [asset_type] => 3
            [chassis_row] => 7
            [chassis_col] => 1
            [manufacturer] => 3Com
            [model] => 3C16791C
        )

    [8] => Array
        (
            [name] => 
            [id] => 2
            [asset_type] => 2
            [chassis_row] => 8
            [chassis_col] => 1
            [manufacturer] => Aculab
            [model] => E1/T1 PCI
        )

)

How can I verify the chassis_row, and chassis_col are not the same in any of the indexes?
he 0, and 1 both have the same values (chassis_row, and chassis_col), would this be the right line of thinking…

foreach($assetArray as $asset) {
  if($asset[chassis_col] == $asset+1[chassis_col] && $asset[chassis_row] == $asset+1[chassis_row])

Not sure what the $asset+1 stuff is about. Maybe you are a C/C++ dev? You could loop through and record each chassis_row and chassis_col in an “already seen” list and then compare each asset to the list to see if it is found already. This is assuming of course that chassis_row 1 and chassis_col 1 could be seen as a pair on index 7 and not necessarily right after the original pair.

:slight_smile:

Do you only need to know if there are some doubles or do you also need to know which one are double?

I will give basically the same answer as your other post Comparing values in an array with this modified answer.


if(!empty($assetArray)):
	$newdata = array();
	foreach($assetArray as $k => $arr):
		$newdata[$assetArray[$k]['chassis_row']][$assetArray[$k]['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;

You are basically restructuring the array to use the chassis_row as the primary key and the chassis_col as the secondary key and an OPEN key[] to hold array values. This restructuring will look more like this.

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [name] => 
                            [id] => 2
                            [asset_type] => 5
                            [chassis_row] => 1
                            [chassis_col] => 1
                            [manufacturer] => 
                            [model] => 
                        )

                    [1] => Array
                        (
                            [name] => 
                            [id] => 1
                            [asset_type] => 1
                            [chassis_row] => 1
                            [chassis_col] => 1
                            [manufacturer] => Brocade
                            [model] => VDX 2730
                        )

                )

        )//etc...

You will notice that in the second double foreach section as we loop through the chassis_row and chassis_col we then do a count of the array under these keys looking for a count greater then 1 if(count($newdata[$k][$k2]) > 1): (like shown in the array above) and IF found place these arrays into a $collision array, shown below.

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [name] => 
                    [id] => 2
                    [asset_type] => 5
                    [chassis_row] => 1
                    [chassis_col] => 1
                    [manufacturer] => 
                    [model] => 
                )

            [1] => Array
                (
                    [name] => 
                    [id] => 1
                    [asset_type] => 1
                    [chassis_row] => 1
                    [chassis_col] => 1
                    [manufacturer] => Brocade
                    [model] => VDX 2730
                )

        )

)
1 Like

Why loop twice? You’ve already got the mechanism to detect a collision inside the first loop (if $newdata[$assetArray[$k]['chassis_row']][$assetArray[$k]['chassis_col']] already exists, there’s a collision…)

I agree with your logic but how would you grab the values of all colliding items? I guess you could say “the one I already have is an item. THIS current item is another” but would I add it to to $newdata array? …and what if there is another match found later, It would need to be handled differently as you can’t say “the one I already have is an item.”. There would be too many IF conditions where a structured sort and a count can give you all items.

1 Like

Where is this data coming from, because if there can only be one entry per row/column combination, you should prevent the duplicate from being inserted into wherever (array, database table) in the first place?

If this data is being stored in a database table, your database design must enforce uniqueness, by defining the two columns as a composite unique index. You would then just attempt to insert/update the data and detect if the query produced a duplicate index error.

1 Like

if (key exists)
add existing item and new item to collisions.
at the end, take the unique of the collisions array.

i have 1 if, and its inside the first loop?

maybe if you really want to, you can usort the array as well.