Cakephp多维数组

Cake php 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 数组中的 del 下方添加一个带有手动值的字段评级。 请帮我解决这个问题。

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

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

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

Hash 适用于 CakePHP 2 和 3。请记住,在 CakePHP 3 中,您需要处理命名空间,因此上述操作可以按以下方式完成:-

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

如果您需要插入不同的值,那么您将需要遍历数组:-

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