蛋糕php多维数组

时间:2015-07-02 06:34:20

标签: php cakephp multidimensional-array

当我调试发现此输出时,我有一个来自连接查询的数组。 这是我在控制器中的代码

$agetFeatureGigs = $this->Gigs->getFeatureGigs();
$this->set('agetFeatureGigs', $agetFeatureGigs);

$yourValue='7';
foreach($agetFeatureGigs as $key => $val) {
    $agetFeatureGigs[$key]['Gigs']['manual'] = $yourValue;
}

输出

array(
    (int) 0 => array(
        'usersjoin' => array(
            'action' => 'YES'
        ),
        'Gigs' => array(
            'id' => '2',
            'username' => 'nmodi',
            'category' => 'Creativity  & Designing',
            'subcategory' => 'Logo Design',
            'picture' => 'Banner_logo1.jpg',
            'video' => '',
            'title' => 'I will design 2 AWESOME logo design in 48 hours',
            'delivery' => '24 Hrs',
            'workinghrs' => '3',
            'feature' => 'YES',
            'action' => 'YES',
            'del' => 'NO'
        )
    ),
    (int) 1 => array(
        'usersjoin' => array(
            'action' => 'YES'
        ),
        'Gigs' => array(
            'id' => '1',
            'username' => 'ptailor',
            'category' => 'Creativity  & Designing',
            'subcategory' => 'Logo Design',
            'picture' => '128.jpg',
            'video' => 'https://www.youtube.com/watch?v=KtCF5tyAr-o&feature=inp-gs-IOL-07-47',
            'title' => 'I will proofread and edit your document I will proofread and edit your document',
            'delivery' => '12 Hrs',
            'workinghrs' => '2',
            'feature' => 'YES',
            'action' => 'YES',
            'del' => 'NO'
        )
    )
)

但是我需要在gigs数组中添加一个带有手动值的字段评级。  请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果您需要在数据中的每个Gigs数组中插入相同的值,可以使用CakePHP's Hash utility,如下所示: -

Hash::($data, '{n}.Gigs.rating', $value);

$data是您的数组,而$value是您想要使用关联键rating插入的值。

Hash适用于CakePHP 2和3.请记住在Cake 3中你需要处理命名空间,所以上面的操作如下: -

Cake\Utility\Hash::($data, '{n}.Gigs.rating', $value);

如果需要插入不同的值,则需要循环遍历数组: -

foreach ($data as &$item) {
    $item['Gigs']['rating'] = $value;
}