在php中按顺序排列和数组?
我有一个上传多张图片的表格。。表中的字段是在php中按顺序排列和数组?,php,arrays,Php,Arrays,我有一个上传多张图片的表格。。表中的字段是 ++++++++++++++++++++++++++ id,user_id,photo,position ++++++++++++++++++++++++++ 一个用户id最多可以有8张图像 现在如果一个用户上传的图像在假设1,3,5的位置 当我必须在从数据库获取后返回数组时 我将获得1,3,5位置的值,但我必须返回数组,其中包含从1到8的所有位置 因此,根据要求,位置1将具有来自数据库的值1,因为数据库中有值,但位置2没有值,因此在数组中位置2将为
++++++++++++++++++++++++++
id,user_id,photo,position
++++++++++++++++++++++++++
一个用户id最多可以有8张图像
现在如果一个用户上传的图像在假设1,3,5的位置
当我必须在从数据库获取后返回数组时
我将获得1,3,5位置的值,但我必须返回数组,其中包含从1到8的所有位置
因此,根据要求,位置1将具有来自数据库的值1,因为数据库中有值,但位置2没有值,因此在数组中位置2将为空
基本上,数据库中有值的位置将保存数据库中的值,而数据库中没有值的位置将为空
位置是12345678
如何在php中执行此操作?您无法从不存在的表值中获取。所以写这样的代码
$Something = 'test';
$aFromTable = array (1=>$Something,3=>$Something,5=>$Something);
for ($i=1;$i<9;$i++)
{
if (!isset($aFromTable[$i]))
$aFromTable[$i] = $Something;
}
var_dump($aFromTable);
$Something = 'test';
$aStandard[1] = null;
$aStandard[2] = null;
$aStandard[3] = null;
$aStandard[4] = null;
$aStandard[5] = null;
$aStandard[6] = null;
$aStandard[7] = null;
$aStandard[8] = null;
$aFromTable = array (1=>$Something,3=>$Something,5=>$Something);
$aFromTable = $aFromTable + $aStandard;
var_dump($aFromTable);
无法从表中获取不存在的值。所以写这样的代码
$Something = 'test';
$aFromTable = array (1=>$Something,3=>$Something,5=>$Something);
for ($i=1;$i<9;$i++)
{
if (!isset($aFromTable[$i]))
$aFromTable[$i] = $Something;
}
var_dump($aFromTable);
$Something = 'test';
$aStandard[1] = null;
$aStandard[2] = null;
$aStandard[3] = null;
$aStandard[4] = null;
$aStandard[5] = null;
$aStandard[6] = null;
$aStandard[7] = null;
$aStandard[8] = null;
$aFromTable = array (1=>$Something,3=>$Something,5=>$Something);
$aFromTable = $aFromTable + $aStandard;
var_dump($aFromTable);
选择位置为数组关键点的数组 例如
$image[position] = image_name;
代码
现在运行for循环,如下所示
for($i=0;$i<8;$i++){
if(!isset($image[$i])) $image[$i] = '';
}
对于($i=0;$i选择一个位置为数组键的数组
例如
$image[position] = image_name;
代码
现在运行for循环,如下所示
for($i=0;$i<8;$i++){
if(!isset($image[$i])) $image[$i] = '';
}
$i=0;$i的
用于生成行数组的SQL示例:
SELECT photo, position FROM photo_table WHERE user_id = 3
用于生成行数组的SQL示例:
SELECT photo, position FROM photo_table WHERE user_id = 3
关于索引中有间隙的数字索引数组示例,请参见此处:按顺序选择它们?那么user_2
会发生什么情况?ID是否为9->16?@Scuzzy按顺序选择它们意味着什么?(您可以在sql端使用IFNULL
)关于索引中有间隙的数字索引数组示例,请参见此处:按顺序选择它们?那么user_2
会发生什么情况?ID是否为9->16?@Scuzzy按顺序选择它们意味着什么?(您可以在sql端使用IFNULL
)