Need to restructure array into usable array

Hello everyone,

As some of you might know I landed a job at an IT firm not to long ago.
They gave me the assignment to rewrite one of their files.

I am almost done but I am more or less stuck with the last part.

I need to have a array that keeps track how much stuff a branch with a specific ID has bought.
But it is not for one specific ID, it is for an unknown amount.

This is what I have now :

array(38) {
  [1]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1073"
  }
  [2]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1075"
  }
  [3]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1210"
  }
  [4]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1100"
  }
  [5]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [6]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [7]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [8]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [9]=>
  array(2) {
    ["s"]=>
    string(1) "6"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [10]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [11]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1010"
  }
  [12]=>
  array(2) {
    ["s"]=>
    string(1) "4"
    ["filiaalNummer"]=>
    string(4) "1060"
  }
  [13]=>
  array(2) {
    ["s"]=>
    string(1) "9"
    ["filiaalNummer"]=>
    string(4) "1080"
  }
  [14]=>
  array(2) {
    ["s"]=>
    string(1) "4"
    ["filiaalNummer"]=>
    string(4) "1100"
  }
  [15]=>
  array(2) {
    ["s"]=>
    string(1) "3"
    ["filiaalNummer"]=>
    string(4) "1080"
  }
  [16]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1140"
  }
  [17]=>
  array(2) {
    ["s"]=>
    string(1) "6"
    ["filiaalNummer"]=>
    string(4) "1177"
  }
  [18]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1177"
  }
  [19]=>
  array(2) {
    ["s"]=>
    string(1) "4"
    ["filiaalNummer"]=>
    string(4) "1177"
  }
  [20]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1177"
  }
  [21]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1177"
  }
  [22]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1130"
  }
  [23]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1198"
  }
  [24]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1198"
  }
  [25]=>
  array(2) {
    ["s"]=>
    string(1) "5"
    ["filiaalNummer"]=>
    string(4) "1071"
  }
  [26]=>
  array(2) {
    ["s"]=>
    string(1) "1"
    ["filiaalNummer"]=>
    string(4) "1071"
  }
  [27]=>
  array(2) {
    ["s"]=>
    string(1) "6"
    ["filiaalNummer"]=>
    string(4) "1071"
  }
  [28]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1071"
  }
  [29]=>
  array(2) {
    ["s"]=>
    string(1) "7"
    ["filiaalNummer"]=>
    string(4) "1071"
  }
  [30]=>
  array(2) {
    ["s"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1080"
  }
  [31]=>
  array(2) {
    ["a4"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1190"
  }
  [32]=>
  array(2) {
    ["a4"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1050"
  }
  [33]=>
  array(2) {
    ["a4"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1050"
  }
  [34]=>
  array(2) {
    ["a4"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "1100"
  }
  [35]=>
  array(2) {
    ["a4"]=>
    string(1) "4"
    ["filiaalNummer"]=>
    string(4) "1252"
  }
  [36]=>
  array(2) {
    ["a4"]=>
    string(1) "4"
    ["filiaalNummer"]=>
    string(4) "9999"
  }
  [37]=>
  array(2) {
    ["a4"]=>
    string(1) "2"
    ["filiaalNummer"]=>
    string(4) "9999"
  }
  [38]=>
  array(2) {
    ["a4"]=>
    string(2) "20"
    ["filiaalNummer"]=>
    string(4) "9999"
  }
}

I want to merge all subarrays with the same “filiaalNummer”

Already tried using a for loop and a foreach but I cannot get either of them work.
Does somebody know how to push me in the right direction.

this looks like it’s coming out of a database. if you were using PDO you can make PDO do the grouping by using its group fetch mode.

1 Like

No I am making the array from a foreach loop.
I will come back at this in a few my colleage is looking at it with me now.

But here is the code :

$runlogArray[$fArrayCount] = array(
	'filiaalNummer' => (string)$xml->order[0]->besteller[0]->persoon[0]->filiaalnummer[0],
	'details' => array(
		$format['format'] => (string)$xml->order[0]->orderrows[0]->orderrow[0]->aantal[0]
	)	
);

Ah, I thought the array was the source.

But if the source is an XML file, it might be possible to go directly
XML file → final array
rather than
XML file > intermediate array → final array

then you need to use one more loop:

// this is a bit of a guess since I can only make assumptions about the XML structure
foreach ($xml->order as $order) {
    $filiaal = (string) $order->besteller[0]->persoon[0]->filiaalnummer[0];
    $runlogArray[$filiaal][] = [
        'format' => $order->orderrows[0]->orderrow[0]->aantal[0],
    ]
}
1 Like

running now to check the result.

Edit will come soon :thumbsup:


Now I am getting the following response :

array(32) {
  [1280]=>
  array(2) {
    [0]=>
    array(1) {
      ["a3"]=>
      string(1) "1"
    }
    [1]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
  }
  [1073]=>
  array(6) {
    [0]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
    [1]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
    [2]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
    [3]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
    [4]=>
    array(1) {
      ["a4"]=>
      string(1) "2"
    }
    [5]=>
    array(1) {
      ["s"]=>
      string(1) "5"
    }
  }
.... and more.

This looks much more useable.
I will try and throw this to the database and see what happens.

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