Php 读取特定范围内键的升序数组
) 在此数组中,需要将[selectCat1]、[selectSubCat1]、[heading1]、[filename1]、[orderBox1]的值插入DB表的一行,并将[selectCat3]、[selectSubCat3]、[heading3]、[filename3]、[orderBox3]的值插入另一行 我不能在1到7的范围内使用for循环,因为在1到7的范围内,一些键应该被忽略,就像在本例中忽略键2一样 我已尝试使用以下代码Php 读取特定范围内键的升序数组,php,Php,) 在此数组中,需要将[selectCat1]、[selectSubCat1]、[heading1]、[filename1]、[orderBox1]的值插入DB表的一行,并将[selectCat3]、[selectSubCat3]、[heading3]、[filename3]、[orderBox3]的值插入另一行 我不能在1到7的范围内使用for循环,因为在1到7的范围内,一些键应该被忽略,就像在本例中忽略键2一样 我已尝试使用以下代码 Array ( [selectCat1] => 's
Array
(
[selectCat1] => 'somevalue'
[selectSubCat1] => 'somevalue'
[heading1] => 'somevalue'
[filename1] => 'somevalue'
[orderBox1] => 'somevalue'
[selectCat3] => 'somevalue'
[selectSubCat3] => 'somevalue'
[heading3] => 'somevalue'
[filename3] => 'somevalue'
[orderBox3] => 'somevalue'
[selectCat4] => 'somevalue'
[selectSubCat4] => 'somevalue'
[heading4] => 'somevalue'
[filename4] => 'somevalue'
[orderBox4] => 'somevalue'
[selectCat5] => 'somevalue'
[selectSubCat5] => 'somevalue'
[heading5] => 'somevalue'
[filename5] => 'somevalue'
[orderBox5] => 'somevalue'
[selectCat6] => 'somevalue'
[selectSubCat6] => 'somevalue'
[heading6] => 'somevalue'
[filename6] => 'somevalue'
[orderBox6] => 'somevalue'
[selectCat7] => 'somevalue'
[selectSubCat7] => 'somevalue'
[heading7] => 'somevalue'
[filename7] => 'somevalue'
[orderBox7] => 'somevalue'
end($form_data)//$表单_数据位于数组上方
$key=key($form_data);
$last\u entry\u no=substr($key,8)//获取最后一个密钥号
$count=计数($form_数据);
$rowsToInsert=$count/5;
对于($i=1;$i$value){
回显$key.“
”;
}
}
这是行不通的。请帮我做这个试试这样的事情:
end($form_data); //$form_data is above array
$key = key($form_data);
$last_entry_no= substr($key,8); //to get the last key number
$count = count($form_data);
$rowsToInsert = $count/5;
for($i=1; $i<=$rowsToInsert; $i++)
{
foreach ($form_data as $key => $value) {
echo $key.'<br>';
}
}
在$group
数组的每个元素中,您将获得索引键组的键值对
输出:
$group = [];
foreach($array as $key => $value){
$i = substr($key, -1);
$key = rtrim($key, $i);
if(!isset($group[$i])){
$group[$i] = [];
}
$group[$i][$key] = $value;
}
@SagarCh您应该提供数据库信息,如表strucure和使用的数据库引擎名称,以便这里的任何人都可以帮助您。然而,我相信这可能是检查手册的一个原因,或者在这里问另一个问题。如果你相信我的答案对你有帮助,请随意投票或接受它作为答案。非常感谢!这真的很有帮助。
Array
(
[1] => Array // group 1
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
// no group 2
[3] => Array // group 3
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
[4] => Array
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
[5] => Array
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
[6] => Array
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
[7] => Array
(
[selectCat] => somevalue
[selectSubCat] => somevalue
[heading] => somevalue
[filename] => somevalue
[orderBox] => somevalue
)
)