0

I've got an array generated from a SQL query :

<pre>
$df = $bdd->query('select * from V_Customer_Link where ref_customer in (select       ref_customer from V_Customer_Link group by ref_customer having count(*) >=4)');
$result = $df->FetchAll();
$df->closeCursor();
</pre>

When I do a print_r($result) I've got this :

    Array
    (
        [0] => Array
            (
                [docno] => 59
                [0] => 59
                [ref_customer] => ALFKI
            [1] => ALFKI
            [type_de_document] => 2
            [2] => 2
            [TypeDeDoc] => Rib
            [3] => Rib
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=59.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=59.1&child=true
        )

    [1] => Array
        (
            [docno] => 60
            [0] => 60
            [ref_customer] => ALFKI
            [1] => ALFKI
            [type_de_document] => 1
            [2] => 1
            [TypeDeDoc] => Carte Identité
            [3] => Carte Identité
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=60.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=60.1&child=true
        )

    [2] => Array
        (
            [docno] => 61
            [0] => 61
            [ref_customer] => ALFKI
            [1] => ALFKI
            [type_de_document] => 3
            [2] => 3
            [TypeDeDoc] => Kbis
            [3] => Kbis
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=61.2&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=61.2&child=true
        )

    [3] => Array
        (
            [docno] => 62
            [0] => 62
            [ref_customer] => ALFKI
            [1] => ALFKI
            [type_de_document] => 4
            [2] => 4
            [TypeDeDoc] => Contrat
            [3] => Contrat
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=62.2&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=62.2&child=true
        )

    [4] => Array
        (
            [docno] => 66
            [0] => 66
            [ref_customer] => BOLID
            [1] => BOLID
            [type_de_document] => 4
            [2] => 4
            [TypeDeDoc] => Contrat
            [3] => Contrat
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=66.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=66.1&child=true
        )

    [5] => Array
        (
            [docno] => 67
            [0] => 67
            [ref_customer] => BOLID
            [1] => BOLID
            [type_de_document] => 1
            [2] => 1
            [TypeDeDoc] => Carte Identité
            [3] => Carte Identité
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=67.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=67.1&child=true
        )

    [6] => Array
        (
            [docno] => 68
            [0] => 68
            [ref_customer] => BOLID
            [1] => BOLID
            [type_de_document] => 3
            [2] => 3
            [TypeDeDoc] => Kbis
            [3] => Kbis
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=68.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=68.1&child=true
        )

    [7] => Array
        (
            [docno] => 69
            [0] => 69
            [ref_customer] => BOLID
            [1] => BOLID
            [type_de_document] => 5
            [2] => 5
            [TypeDeDoc] => Liasse Fiscale
            [3] => Liasse Fiscale
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=69.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=69.1&child=true
        )

    [8] => Array
        (
            [docno] => 70
            [0] => 70
            [ref_customer] => BOLID
            [1] => BOLID
            [type_de_document] => 2
            [2] => 2
            [TypeDeDoc] => Rib
            [3] => Rib
            [link] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=70.1&child=true
            [4] => http://localhost/TWA/Client/WEB/Viewer/Viewer.aspx?DocNo=70.1&child=true
        )

)

As you can see, there is only two different ref_customer. In the future, there will be more.

I would like to display information from this array like this :

ALFKI : Rib, Carte Identité, Kbis, Contrat

BOLID : Rib, Carte Identité, Kbis, Contrat, Liasse fiscale

If I do a for or a while, the value ref_customer will be repeated each time. How can I limit this value once and get all the other?

halfer
  • 19,824
  • 17
  • 99
  • 186
Chris
  • 927
  • 1
  • 8
  • 13
  • 1
    you probably want to re-write your query to get the information you need, all attacked to the customer_ref you want. worst case, if that is not an option, just take every row, check what's in there, and build your own new array per customer_ref – Viridis Dec 10 '13 at 15:31
  • This question is interesting to solve but should be simplified if possible. Unnecessesary complexity is evil. – Prashank Dec 10 '13 at 15:37

3 Answers3

1

I'd create another array based on ref_customer and implode the values.

untested, but something along these lines

$ref_customers = array();
foreach($result as $r) {
    $ref_customers[$r['ref_customer']][] = $r['TypeDeDoc'];
}

foreach($ref_customers as $ref => $typededoc) {
    echo $ref . ' : ' . implode(", ", $typededoc);
}
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
0

You need to individually collect your values from the base array. This can be done by iterating over the array values and storing them in another array.

You could do something like this (untested, but should work):

$return = array();

foreach($result as $r) {
    $key = $r['ref_customer'];
    $return[$key][] = $r['TypeDeDoc'];

    ////  and even this would work:
    // if (!array_key_exists($key, $return)) $return[$key] = array();
    // array_push($return[$key], $r['TypeDeDoc']);
}

foreach($return as $k => $v) {
    echo $k . ":" . implode(", ", $v);
}
Stoic
  • 10,536
  • 6
  • 41
  • 60
  • This won't work because "ref_customer" can be inside another array (see my answer). You need to recursively check the $r and fetch that result. – Michael Villeneuve Dec 10 '13 at 15:43
0

Use a simple foreach() and organise your array how you would need it. For example :

$newArray = array();
foreach ( $yourArray as $record )
{
    $result = array_key_exists_r('ref_customer', $record); // use the function from the answer below
    $document = $record['TypeDeDoc'];
    $newArray[$result][] = $document;
}

Your new array will look like this :

["Alfki"] => Array
    (
        [0] => "Carte identité"
        [1] => "Contrat"
        [2] => "Autre document"
    )

Finding the key in a recursive way

Since you don't really know where the key is, you need to use a recursive function. There is a really good example with this answer.

Community
  • 1
  • 1
Michael Villeneuve
  • 3,933
  • 4
  • 26
  • 40
  • @Chris don't forget to upvote/select the answer you used. It will help futur user to see what's working and what answer you used to solve your question. – Michael Villeneuve Dec 10 '13 at 15:59