PHP Array neu Anordnen

bosko

Member
Hallo,

mal wieder meine geliebten array's, meine array ausgabe sieht so aus:

Code:
Array
(
    [0] => Array
        (
            [1] => Array
                (
                    [3] => 17
                )

            [26] => Array
                (
                    [3] => 1
                )

            [2] => Array
                (
                    [3] => 5
                )

            [3] => Array
                (
                    [3] => 4
                )

            [4] => Array
                (
                    [3] => 2
                )

            [5] => Array
                (
                    [3] => 2
                )

            [22] => Array
                (
                    [3] => 2
                )

            [6] => Array
                (
                    [3] => 12
                )

            [7] => Array
                (
                    [3] => 2
                )

            [23] => Array
                (
                    [3] => 2
                )

            [8] => Array
                (
                    [3] => 2
                )

            [9] => Array
                (
                    [3] => 9
                )

            [10] => Array
                (
                    [3] => 1
                )

            [11] => Array
                (
                    [3] => 43
                )

            [12] => Array
                (
                    [3] => 46
                )

            [24] => Array
                (
                    [3] => 2
                )

            [13] => Array
                (
                    [3] => 3
                )

            [14] => Array
                (
                    [3] => 7
                )

            [15] => Array
                (
                    [3] => 4
                )

            [16] => Array
                (
                    [3] => 3
                )

            [17] => Array
                (
                    [3] => 4
                )

            [18] => Array
                (
                    [3] => 1
                )

            [19] => Array
                (
                    [3] => 11
                )

            [20] => Array
                (
                    [3] => 16
                )

            [25] => Array
                (
                    [3] => 1
                )

            [21] => Array
                (
                    [3] => 9
                )

        )

    [1] => Array
        (
            [1] => Array
                (
                    [4] => 0
                )

            [26] => Array
                (
                    [4] => 0
                )

            [2] => Array
                (
                    [4] => 0
                )

            [3] => Array
                (
                    [4] => 0
                )

            [4] => Array
                (
                    [4] => 2
                )

            [5] => Array
                (
                    [4] => 1
                )

            [22] => Array
                (
                    [4] => 0
                )

            [6] => Array
                (
                    [4] => 0
                )

            [7] => Array
                (
                    [4] => 0
                )

            [23] => Array
                (
                    [4] => 0
                )

            [8] => Array
                (
                    [4] => 0
                )

            [9] => Array
                (
                    [4] => 0
                )

            [10] => Array
                (
                    [4] => 0
                )

            [11] => Array
                (
                    [4] => 1
                )

            [12] => Array
                (
                    [4] => 0
                )

            [24] => Array
                (
                    [4] => 0
                )

            [13] => Array
                (
                    [4] => 0
                )

            [14] => Array
                (
                    [4] => 0
                )

            [15] => Array
                (
                    [4] => 0
                )

            [16] => Array
                (
                    [4] => 0
                )

            [17] => Array
                (
                    [4] => 0
                )

            [18] => Array
                (
                    [4] => 0
                )

            [19] => Array
                (
                    [4] => 0
                )

            [20] => Array
                (
                    [4] => 1
                )

            [25] => Array
                (
                    [4] => 0
                )

            [21] => Array
                (
                    [4] => 0
                )

        )

)

nachdem ich diese per PHP versucht habe neu anzuordnen mit:
PHP:
foreach ($a as $b => $c) {
	  foreach ($c as $d => $e) {
	  	$arr[$d][$b] = $e;
	  }
  }
schaut das ganze so aus:
Code:
Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [3] => 17
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [26] => Array
        (
            [0] => Array
                (
                    [3] => 1
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [3] => 5
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [3] => 4
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [4] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 2
                )

        )

    [5] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 1
                )

        )

    [22] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [3] => 12
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [7] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [23] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [8] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [9] => Array
        (
            [0] => Array
                (
                    [3] => 9
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [10] => Array
        (
            [0] => Array
                (
                    [3] => 1
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [11] => Array
        (
            [0] => Array
                (
                    [3] => 43
                )

            [1] => Array
                (
                    [4] => 1
                )

        )

    [12] => Array
        (
            [0] => Array
                (
                    [3] => 46
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [24] => Array
        (
            [0] => Array
                (
                    [3] => 2
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [13] => Array
        (
            [0] => Array
                (
                    [3] => 3
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [14] => Array
        (
            [0] => Array
                (
                    [3] => 7
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [15] => Array
        (
            [0] => Array
                (
                    [3] => 4
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [16] => Array
        (
            [0] => Array
                (
                    [3] => 3
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [17] => Array
        (
            [0] => Array
                (
                    [3] => 4
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [18] => Array
        (
            [0] => Array
                (
                    [3] => 1
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [19] => Array
        (
            [0] => Array
                (
                    [3] => 11
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [20] => Array
        (
            [0] => Array
                (
                    [3] => 16
                )

            [1] => Array
                (
                    [4] => 1
                )

        )

    [25] => Array
        (
            [0] => Array
                (
                    [3] => 1
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

    [21] => Array
        (
            [0] => Array
                (
                    [3] => 9
                )

            [1] => Array
                (
                    [4] => 0
                )

        )

)

möchte aber das es so aussieht:
Code:
Array
(
    [1] => Array
        (
            [3] => 17,
            [4] => 0
        )

    [26] => Array
        (
           [3] => 1,
           [4] => 0
        )

    [2] => Array
        (
            [3] => 5,
            [4] => 0

        )

    [3] => Array
        (
            [3] => 4,
            [4] => 0

        )

wie stelle ich das am besten an? Das die erste zahl (1, 26, 2, 3 ....) so durcheinander ist, ist von mir so gewünscht...

gruß
 
Was machst du denn da komisches die letzte Zeit? Irgendwie sieht mir das danach aus, dass du deine Datenstrukturen mal sauber aufbauen solltest.

Wenn du den Index $b nicht haben will, darfst du ihn natürlich auch nicht verwenden. Wenn du aber dann auf der letzten Ebene einen Index doppelt hast, verlierst du einen Wert.

Code:
foreach ($a as $b => $c) {
      foreach ($c as $d => $e) {
          if (array_key_exists($d, $arr)){
            $arr[$d] += $e;
          }
          else {
            $arr[$d] = $e;
          }
      }
  }
- ungetestet.
 
Zurück
Oben