php – Get a value one time from array-ThrowExceptions

Exception or error:

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?

How to solve:

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);
}

Answer:

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);
}

Answer:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *