读取一定范围内键的升序排列

时间:2014-08-25 09:38:22

标签: php

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'

在[selectCat1],[selectSubCat1],[heading1],[filename1],[orderBox1]的数组值中需要插入一行DB表,[selectCat3],[selectSubCat3],[heading3] ,[filename3],[orderBox3]在另一行。

我不能在1到7的范围内使用for循环,因为在1到7中应该忽略一些键,就像在这种情况下键2被忽略一样。

我尝试使用以下代码...

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>';
  }
}

这不起作用。请帮帮我这个

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$group = [];

foreach($array as $key => $value){
    $i   = substr($key, -1);
    $key = rtrim($key, $i);

    if(!isset($group[$i])){
        $group[$i] = [];
    }

    $group[$i][$key] = $value;
}

$group数组的每个元素中,您将获得索引键组的键值对。

<强>输出:

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
        )

)