-1
$galleryData = [];  
foreach($input['gallery'] as $galleryImg) {


}

我想推入galleryData 数组,一个键控数组。

我怎样才能做到这一点?

我试过了:

$galleryData[] = ['name'=>$galleryImg['file']['name'], 'comment'=> $galleryImg['file']['comment'], 'youtube'=> $galleryImg['file']['youtube']];

但没有运气。

4

2 回答 2

1
$galleryData[] = array(
    'name'=>$galleryImg['file']['name'], 
    'comment'=> $galleryImg['file']['comment'],       
    'youtube'=> $galleryImg['file']['youtube']
);

应该管用

于 2013-09-27T16:18:03.193 回答
0

Try this:

$galleryData = array_map(function($a) {return $a['file'];},$input['gallery']);

If there are other keys, try this variant:

$allowedKeys = array("name","comment","youtube");
$galleryData = array_map(function($a) use ($allowedKeys) {
    return array_intersect_key($a['file'],array_flip($allowedKeys));
},$input['gallery']);

Taking into consideration your update, there's no longer such a shortcut. Try this:

$galleryData = array_map(function($a) {
    return array(
        "comment"=>$a['comment'],
        "name"=>$a['file']['name'],
        "youtube"=>$a['file']['youtube']
    );
},$input['gallery']);
于 2013-09-27T16:19:24.193 回答