如何在PHP中按索引对发布的数组名称进行分组

时间:2016-11-14 10:35:17

标签: php multidimensional-array

我目前正在开展一个项目,当点击“添加”按钮时,字段会缩放 我正在对每个字段进行分组:name="packaging[]"name="packaging[1]"name="packaging[2]"等等。当我提交表单时,这就是发布时数据的样子:

Array
(
    [packaging] => Array
        (
            [0] => 1
            [1] => 2
        )

    [quantity] => Array
        (
            [0] => 1
            [1] => 2
        )

    [total-weight] => Array
        (
            [0] => 1
            [1] => 2
        )

    [length] => Array
        (
            [0] => 1
            [1] => 2
        )

)

使用PHP我想将上面的代码转换成如下所示:

Array
(
    [0] => Array
        (
            [packaging] => 1,
            [quantity] => 1,
            [total-weight] => 1,
            [length] => 1,
        )
    [1] => Array
        (
            [packaging] => 2,
            [quantity] => 2,
            [total-weight] => 2,
            [length] => 2,
        )
)

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

试试这个......

$array=array();
foreach($data as $key=>$value){
    foreach($value as $k=>$val){
         $array[$k][$key]=$val;
    }
}

DEMO

答案 1 :(得分:1)

试试这段代码:

let fromDate = // the date after which you want to retrieve the photos
let toDate // the date until which you want to retrieve the photos 

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "creationDate > %@ && creationDate < %@", fromDate as CVarArg, toDate as CVarArg)

//Just a way to set order
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
options.sortDescriptors = [sortDescriptor]

return PHAsset.fetchAssets(with: .image, options: options)