cakeCakephp多维数组



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

$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数组中添加一个手动值低于del的字段评级。请帮我解决这个问题。

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

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

其中$data是您的数组,$value是您要插入的值,关联键为rating

Hash将同时适用于CakePHP 2和3。只需记住,在Cake 3中,你需要处理名称空间,这样以上操作就可以完成:-

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

如果你需要插入不同的值,那么你需要在数组上循环:-

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

相关内容

  • 没有找到相关文章

最新更新