数组中对象的名称导致顺序发生变化
Name of object in array causes a change in order
使用以下代码:
//turn items into an array
$item_array = array('abc','xyz2','Good','nice-b');
//implode items, turn into string
$item_implode = join("','", $item_array);
//declare an overall array for result
$product_items = array();
$productList = array();
$result = $mysqli->query("SELECT Name, WebsitePrice as price, WebsiteStock as stock from table_products where Name IN ('$item_implode')");
if ($result->num_rows > 0) {
$x = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$x]["Name"] = $row['Name'];
$product_items[$x]["price"] = $row['price'];
$product_items[$x]["stock"] = $row['stock'];
$x = $x + 1;
}
} else {
echo "0 results";
}
我得到这个输出:
abc- 99 - yes
xyz - 20 - yes
Good - 30 - yes
nice-b - 55 - yes
但是当我使用名为 Hello1
而不是 Good
的项目时,像这样:
$item_array = array('abc','xyz2','Hello1','nice-b');
我得到这个输出:
abc- 99 - yes
Hello1 - 77 - yes
xyz - 20 - yes
nice-b - 55 - yes
意思是对象的名字引起了数组顺序的一些变化,变成了second项,尽管它应该是 第三个。
这是什么原因造成的?
In the SQL world, order is not an inherent property of a set of data. Thus, you get no guarantees from your RDBMS that your data will come back in a certain order -- or even in a consistent order -- unless you query your data with an ORDER BY clause.
无法保证 MySQL 将 return 结果按照您在 IN
子句中设置 ID 的顺序。
稍后编辑:根据您最后的评论,您可以这样做:
if ($result->num_rows > 0) {
$product_items = array_flip($item_array);
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$row['Name']] = array();
$product_items[$row['Name']]["Name"] = $row['Name'];
$product_items[$row['Name']]["price"] = $row['price'];
$product_items[$row['Name']]["stock"] = $row['stock'];
}
}
在查询中使用 ORDER BY FIELD(Name, 'abc','xyz2','Good','nice-b');
。您可以使用 $item_implode
来实现可重用性。
[摘自评论]
使用以下代码:
//turn items into an array
$item_array = array('abc','xyz2','Good','nice-b');
//implode items, turn into string
$item_implode = join("','", $item_array);
//declare an overall array for result
$product_items = array();
$productList = array();
$result = $mysqli->query("SELECT Name, WebsitePrice as price, WebsiteStock as stock from table_products where Name IN ('$item_implode')");
if ($result->num_rows > 0) {
$x = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$x]["Name"] = $row['Name'];
$product_items[$x]["price"] = $row['price'];
$product_items[$x]["stock"] = $row['stock'];
$x = $x + 1;
}
} else {
echo "0 results";
}
我得到这个输出:
abc- 99 - yes
xyz - 20 - yes
Good - 30 - yes
nice-b - 55 - yes
但是当我使用名为 Hello1
而不是 Good
的项目时,像这样:
$item_array = array('abc','xyz2','Hello1','nice-b');
我得到这个输出:
abc- 99 - yes
Hello1 - 77 - yes
xyz - 20 - yes
nice-b - 55 - yes
意思是对象的名字引起了数组顺序的一些变化,变成了second项,尽管它应该是 第三个。
这是什么原因造成的?
In the SQL world, order is not an inherent property of a set of data. Thus, you get no guarantees from your RDBMS that your data will come back in a certain order -- or even in a consistent order -- unless you query your data with an ORDER BY clause.
无法保证 MySQL 将 return 结果按照您在 IN
子句中设置 ID 的顺序。
稍后编辑:根据您最后的评论,您可以这样做:
if ($result->num_rows > 0) {
$product_items = array_flip($item_array);
// output data of each row
while($row = $result->fetch_assoc()) {
$product_items[$row['Name']] = array();
$product_items[$row['Name']]["Name"] = $row['Name'];
$product_items[$row['Name']]["price"] = $row['price'];
$product_items[$row['Name']]["stock"] = $row['stock'];
}
}
在查询中使用 ORDER BY FIELD(Name, 'abc','xyz2','Good','nice-b');
。您可以使用 $item_implode
来实现可重用性。
[摘自评论]