如何在PHP中为array_unique将字符串转换为数组

时间:2017-05-04 02:39:39

标签: php sql

如何从数组中的MYSQL转换我的值字符串?所以我可以使用array_unique。因为我在一列中有多个副本。当我回应它时。它以PHP显示所有内容。我尝试了DISTINCT,但它只返回一列而不返回其他列。

enter image description here 不断重复这个名字(BOLD RED)。

代码:

$querysql= "SELECT * FROM `stock` WHERE ID = '$id' ";

$fetchdata = $database->query($querysql);

while($rows= $fetchdata->fetch_assoc()) {

echo "<div id='companylocal'>";
    echo $rows['Company_Locate'];
echo "</div>";
}

3 个答案:

答案 0 :(得分:0)

尝试在查询中使用GROUP BY

$querysql= "SELECT * FROM `stock` WHERE ID = '$id' GROUP BY Company_Locate";

答案 1 :(得分:0)

试试这个,

$querysql= "SELECT * FROM `stock` WHERE ID = '$id' ";

$fetchdata = $database->query($querysql);

while($row= $fetchdata->fetch_assoc()) {
    $rows[] = $row['Company_Locate'];
}
$rows = array_unique($rows);
print_r($rows);

答案 2 :(得分:0)

表名:测试

| id | colA | colB |

| 5 |值5 |价值C |

| 4 |价值4 |价值B |

| 3 |价值3 |价值B |

| 2 |价值2 |价值B |

| 1 |价值1 |价值A |

从上面的示例中。 column(colA)具有唯一值,而column(colB)具有重复值。 按列(colB)对查询进行分组将返回列(colB)

中具有唯一值的行

SQL: SELECT id,colA,colB FROM test GROUP BY colB 返回:

| id | colA | colB |

| 1 |价值1 |价值A |

| 2 |价值2 |价值B |

| 5 |值5 |价值C |

要获得更好的解释,请查看https://www.w3schools.com/sql/sql_groupby.asp

$DataFromDatabase = [ ['id' => 1,
                   'colA' => "value 1",
                   'colB' => "value A"
                  ],
                  ['id' => 2,
                   'colA' => "value 2",
                   'colB' => "value B"
                  ],
                  ['id' => 3,
                   'colA' => "value 3",
                   'colB' => "value B"
                  ],
                  ['id' => 4,
                   'colA' => "value 4",
                   'colB' => "value B"
                  ],
                  ['id' => 5,
                   'colA' => "value 5",
                   'colB' => "value C"
                  ]
                ];
    echo "Data FROM database<br/>";

echo "<pre>"; print_r($DataFromDatabase); echo "</pre>";

//Unique Array $UniqueArray = [];

//Loop the Database result
foreach($DataFromDatabase AS $data){
    //Make the unique value your key for your array
    $UniqueArray[$data['colB']][] = ['id' => $data['id'],
                                     'colA' => $data['colA']
                                    ];
}

echo "Unique Array<br/>";
echo "<pre>";
print_r($UniqueArray);
echo "</pre>";

您的uniqueArray的结果将是 像这样的东西:

Array
(
    [value A] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [colA] => value 1
                )

        )

    [value B] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [colA] => value 2
                )

            [1] => Array
                (
                    [id] => 3
                    [colA] => value 3
                )

            [2] => Array
                (
                    [id] => 4
                    [colA] => value 4
                )

        )

    [value C] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [colA] => value 5
                )

        )

)

值堆叠在每个唯一列(colB)值上。 现在由您决定如何打印数组的值