Zagwozdka z tablicami

0

Witam,

Muszę wyrzucić z tablicy tablice, w której brakuje danych pomiędzy.

Przykładowe dane:

Array
(
	[key1] => Array
	(
	[1] => Array
		(
			[0] => 
			[1] => cos-LV0
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[3] => Array
		(
			[0] => 
			[1] => cos-LV1.5
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[4] => Array
		(
			[0] => 
			[1] => cos-LV2
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[5] => Array
		(
			[0] => 
			[1] => cos-LV4
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[6] => Array
		(
			[0] => 
			[1] => cos-LV5
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[7] => Array
		(
			[0] => 
			[1] => cos-Lv6
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[8] => Array
		(
			[0] => 
			[1] => cos-LV7
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[9] => Array
		(
			[0] => 
			[1] => cos-LV8
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	)

	[key2] => Array
	(
	[1] => Array
		(
			[0] => 
			[1] => ktos-LV0
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[3] => Array
		(
			[0] => 
			[1] => ktos-LV1.5
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[4] => Array
		(
			[0] => 
			[1] => ktos-LV2
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[5] => Array
		(
			[0] => 
			[1] => ktos-LV4
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[6] => Array
		(
			[0] => 
			[1] => ktos-LV5
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[7] => Array
		(
			[0] => 
			[1] => ktos-LV6
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[8] => Array
		(
			[0] => 
			[1] => ktos-LV7
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	[9] => Array
		(
			[0] => 
			[1] => ktos-LV8
			[2] => 
			[3] => 
			[4] => 
			[5] =>
		)

	)

) 

Mój kod, który niestety nie usuwa błędnych tablic(takich, które powinien)

protected function removeArrayIfThereIsMissingLevel($a){
        $count = count($a);
        $tab = [];
        for($i=0; $i<$count-1; $i++){
           if(isset($a[$i]) && isset($a[$i+1])){
              $from1 = stripos($a[$i][1],'lv');
              $from2 = stripos($a[$i+1][1],'lv');
              $to1 = stripos($a[$i+1][1],'.');
              $to2 = stripos($a[$i+1][1],'.');
              if($from1!=false && $from2!=false){
                 $stringToCheck1 = substr($a[$i][1],$from1+2,$to1+1);
                 $stringToCheck2 = substr($a[$i+1][1],$from2+2,$to2+1);
                 if($stringToCheck1+1 == $stringToCheck2){
                    $tab[$a[$i][2]] = true;
                 } else {
                    return false;
                 }
              }
           }
        }
        return true;
      }

Mógłbym prosić o pomoc?

Z góry dziękuje

0

Co to znaczy "w której brakuje danych pomiędzy"?

0

W danych powyżej brakuje tablicy gdzie [1] => cos-LV3 lub [1] => ktos-LV3

0

w jaki sposób te stringi są generowane? zawsze tam jest "cos-LV[numer]" ?

0

No tak, ale nie ma też na przykład LV1 (jest LV1.5) - chyba, że część \..* można zignorować?

Coś a'la:

//sorted data assumed
function hasMissingData(array $array) {
    reset($array);
    $matches = [];
    $regex = '/.*?-LV(\\d+)\\.*/';
    preg_match($regex, current($array), $matches); // good data assumed
    $assumed = (int)$matches[1];
    $step = 1;
    while (next($array)) {
        preg_match($regex, current($array), $matches);
        $current = (int)$matches[1];
        if (($assumed += $step) != $current) {
            return true;
        }
    }
    
    return false;
}

Oczywiście musiałbyś sobie to dostosować do swojej tablicy.

0
dzek69 napisał(a):

w jaki sposób te stringi są generowane? zawsze tam jest "cos-LV[numer]" ?

Generalnie tak, ale cos jest zmienne i może się zdarzyć że -LV[numer] nie będzie wgl

Xupicor napisał(a):

No tak, ale nie ma też na przykład LV1 (jest LV1.5) - chyba, że część \..* można zignorować?....

Generalnie jest tak że mamy albo dane typu LV0, LV1.4, LV2(więc 1 już jest) albo LV0, LV1, LV2

1 użytkowników online, w tym zalogowanych: 0, gości: 1