所以我有这个图像数组拉,数组的键只有0,1,2,3,4,5 ….等等……
如何将该表的“id”列中的值作为键,并将“link”作为值.
关联数组,没有?
这是我的PHP:
$myImageID = $me['imageid'];
$findImages = "SELECT link FROM images WHERE model_id ='{$me['id']}'";
$imageResult = MysqL_query($findImages) or die (MysqL_error());
$myImages = array();
while($row = MysqL_fetch_array($imageResult)) {
$myImages[] = $row[0];
}
这是我有的:
{
[0] -> "http://website.com/link.jpg"
[1] -> "http://website.com/li123nk.jpg"
[2] -> "http://website.com/link3123.jpg"
}
这就是我想要的:
{
[47] -> "http://website.com/link.jpg"
[122] -> "http://website.com/li123nk.jpg"
[4339] -> "http://website.com/link3123.jpg"
}
最佳答案
只需选择id并使其成为数组的关键.就这么简单.
原文链接:https://www.f2er.com/mysql/433142.html$findImages = "SELECT id,link FROM images WHERE model_id ='{$me['id']}'";
$imageResult = MysqL_query($findImages) or die (MysqL_error());
$myImages = array();
while($row = MysqL_fetch_array($imageResult)) {
$myImages[$row[0]] = $row[1];
}
仅供参考,you shouldn’t use mysql_*
functions in new code.他们不再维持and are officially deprecated.见red box?请转而了解prepared statements,使用PDO或MySQLi – this article将帮助您确定哪个.如果您选择PDO,here is a good tutorial.
你也大开到SQL injections