Filter associative php array

Serghei

I need to filter my php array of arrays and make it unique. Here is my array:

array (size=50)
  0 => 
    array (size=1)
      'circuit_reference' => string '1' (length=1)
  1 => 
    array (size=2)
      'circuit_reference' => string '1' (length=1)
      'circuit_phase' => string 'L1' (length=2)
  2 => 
    array (size=3)
      'circuit_reference' => string '1' (length=1)
      'circuit_phase' => string 'L1' (length=2)
      'circuit_is_sub_main' => string 'N' (length=1)
  3 => 
    array (size=1)
      'circuit_reference' => string '1' (length=1)
  4 => 
    array (size=2)
      'circuit_reference' => string '1' (length=1)
      'circuit_phase' => string 'L2' (length=2)

I need to have in new array only subarrays with max counts elements:

array(
  1 => 
    array (size=3)
      'circuit_reference' => string '1' (length=1)
      'circuit_phase' => string 'L1' (length=2)
      'circuit_is_sub_main' => string 'N' (length=1)
  2 => 
   array (size=2)
      'circuit_reference' => string '1' (length=1)
      'circuit_phase' => string 'L2' (length=2)

Stucked with answer(

Serghei

I solved this tasked myself, here is answer:

            foreach ($arrays as $array) {
                if (isset($array['circuit_reference']) && isset($array['circuit_phase'])) {
                    if ($curr == '' && $curt == '') {
                        $curr = $array['circuit_reference'];
                        $curt = $array['circuit_phase'];
                    }
                    if ($curr != $array['circuit_reference'] || $curt != $array['circuit_phase']) {
                        $curr = $array['circuit_reference'];
                        $curt = $array['circuit_phase'];
                        $resulted_circuits[] = $arrays[$maxin];
                        $countm = 0;
                        $maxin = 0;
                    }
                    if ($countm < count($array)) {
                        $countm = count($array);
                        $maxin = $i;
                    }
                }
                $i++;
            }
            if ($maxin != 0 && $maxin <= count($arrays)) {
                $resulted_circuits[] = $arrays[$maxin];
            }

Thanks to everybody.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related