Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP循环中显示所有唯一的行_Php - Fatal编程技术网

在PHP循环中显示所有唯一的行

在PHP循环中显示所有唯一的行,php,Php,我在PHP中有一个foreach循环: $Directli = new Directli(); $output = $Directli->build_query('getPayments'); foreach($output as $row) { } 我想回显循环中的信息,但只回显基于$row->ID签出的每个唯一行。您可以在循环之前调用它,以获取具有唯一项的数组。如果无法使用它,则必须在循环中构建哈希,以检查该项是否已被处理: $map = array(); foreach($out

我在PHP中有一个foreach循环:

$Directli = new Directli();
$output = $Directli->build_query('getPayments');
foreach($output as $row) {

}
我想回显循环中的信息,但只回显基于
$row->ID

签出的每个唯一行。您可以在循环之前调用它,以获取具有唯一项的数组。如果无法使用它,则必须在循环中构建哈希,以检查该项是否已被处理:

$map = array();
foreach($output as $row) {
 if (!isset($map[$row->ID])) {
    $map[$row->ID] = true;
    echo $row->ID;
 }
}

请确保您的问题格式符合您的代码、示例和理论

<?php 
.. // your connections etc .. //
$sql = mysql_query("SELECT * FROM `something` WHERE `id`='$id'"); // $id you are giving here $row->ID
$res = mysql_fetch_array($sql);
$output = array($res);
foreach($output as $row => $result)
{
echo $result;

}
?>

作为替代方案,您可以创建一个容器来保存ID,然后检查循环内部,如果它还没有在循环内部,则将其推到内部

$ids = array();
foreach($output as $row) {
    if(!in_array($row->ID, $ids)) {
        $ids[] = $row->ID;
        // other process below that you need to do
        echo $row->ID;
    }
}

如果此结果数组来自<代码>选择< /COD>语句,请考虑使用<代码>区分< /COD> >子句。 抱歉,您需要什么?要显示循环返回的每一行,但仅显示唯一的行(其中ID是唯一的),您可能需要使用

array\u unique
:使用array\u unique删除重复项。(抱歉@naota在你发表评论的时候写的)在他的编辑之后,我猜他是在第二个案例中。他应该用所有处理过的id构建一个单独的id。
**use array_unique:**

$result = array_unique($input);
print_r($result);
where $input = array("a" => "green", "red", "b" => "green", "blue", "red");  //for example
**use array_unique:**

$result = array_unique($input);
print_r($result);
where $input = array("a" => "green", "red", "b" => "green", "blue", "red");  //for example