从数组中获取一次值

时间:2013-12-10 15:24:25

标签: php arrays

我有一个从SQL查询生成的数组:

<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>

当我执行print_r($ result)时,我得到了这个:

<pre>
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
    )

如您所见,ref_customer只有2个不同。在未来,会有更多。

我想从这个数组中发送信息:

ALFKI:Rib,CarteIdentité,Kbis,Contrat

BOLID:Rib,CarteIdentité,Kbis,Contrat,Liasse fiscale

如果我做了一段时间或者一段时间,每次都会重复值ref_customer。

如何限制此值一次并获取所有其他值。

我希望我的描述足够清楚。

感谢。

3 个答案:

答案 0 :(得分:1)

我会根据ref_customerimplode值创建另一个数组。

未经测试,但这些内容

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

foreach($ref_customers as $ref => $typededoc) {
    echo $ref . ' : ' . implode(", ", $typededoc);
}

答案 1 :(得分:0)

您需要从基础数组中单独收集值。这可以通过迭代数组值并将它们存储在另一个数组中来完成。

你可以这样做(未经测试,但应该有效):

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

答案 2 :(得分:0)

使用简单的foreach()并根据需要组织数组。例如:

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

您的新阵列将如下所示:

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

以递归方式查找密钥

由于您实际上并不知道密钥的位置,因此需要使用递归函数。 this answer有一个很好的例子。