0

I have a question, because I can't figure it out and I can't find it anywhere (maybe the wrong search-words I don't know..). I have the following:

Array
(
    [0] => Array
        (
            [Factuurnummer] => 50
            [Omschrijving] => Thing 1
        )

    [1] => Array
        (
            [Factuurnummer] => 50
            [Omschrijving] => Thing 2
        )

    [2] => Array
        (
            [Factuurnummer] => 51
            [Omschrijving] => Thing 2
        )

    [3] => Array
        (
            [Factuurnummer] => 51
            [Omschrijving] => Thing 3
        )

    [4] => Array
        (
            [Factuurnummer] => 51
            [Omschrijving] => Thing 4
        )

)

Now when I use a foreach to print it all in a table I get 5 rows, but I only want 2, based on the same 'Factuurnummer'. So 1 row with 'Factuurnummer'=50 and 'Omschrijving'=Thing 1, Thing 2 and another row with 'Factuurnummer'=51 and 'Omschrijving'=Thing 2, Thing 3, Thing 4.

I've read something about array_intersect, but I don't know if that will help in this case. The nicest would be if the value from 'Omschrijving' based on same 'Factuurnummer' would be separated with a comma (like my little example).

Hope I was clear enough and someone could help me in the right direction!

user3050534
  • 227
  • 4
  • 17

2 Answers2

0

You should rewrite your array before you print it, assuming above array is called $data:

$facturen = array();
foreach($data as $row) {
    $factuurnummer = $row['Factuurnummer'];
    if (!isset($facturen[$factuurnummer])) {
        $facturen[$factuurnummer] = array();
    }
    $facturen[$factuurnummer][] = $row['Omschrijving'];
}

// On display:
foreach($facturen as $factuurnummer => $omschrijvingen) {
     echo $factuurnummer.': '.implode(', ', $omschrijvingen);
}
Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
  • Thank you very much for this, it does exactly what i mean. But next problem, originally my arrays are extended beside 'Facuurnummer' and 'Omschrijving' with a column 'Naam' and 'Datum' (are equal with same Factuurnummer). So on screen it must be: Factuurnummer | Naam | Datum | Omschrijving. With your code I cant get the corresponding Naam and Datum. How do I do that? Hope you understand me :) – user3050534 Nov 29 '13 at 20:48
  • Assuming invoices with same number have same name and date (only description differs): Change `$facturen[$factuurnummer] = array()` into `$facturen[$factuurnummer] => array('Naam' => $row['Naam'], 'Datum' => $row['Datum'], 'Omschrijvingen' => array())`, and `$facturen[$factuurnummer]['Omschrijvingen'][] = $row['Omschrijving']` – Peter van der Wal Nov 29 '13 at 20:55
  • Thank you very very much! It workes like a train now :D. Great! – user3050534 Nov 29 '13 at 21:30
0

You can easily use array_walk() to write your data into an array with more usable format:

$new_array = array();
array_walk($existing_array, function($item, $key_not_used) use ($new_array) {
    $new_array[$item['Factuurnummer']][] = $item['Omschrijving'];
});
var_dump($new_array);
Mike Brant
  • 70,514
  • 10
  • 99
  • 103