每个复杂的检索数组值

时间:2014-06-27 20:50:02

标签: php arrays codeigniter multidimensional-array

我目前正在开发一个为多个维度创建数组的项目。 print_r()会导致:

Array ( 
[campaigns] => Array ( 
    [0] => Array ( 
        [CampaignId] => 1 
        [DID] => 2394434444 
        [FWDDID] => 3214822821 
        [ProductId] => 1 
        [CampaignName] => Fort Myers Bus #1 
        [ProductName] => CallTrack - Sharktek 
        [Active] => 1 [CompanyId] => 1 ) 

    [1] => Array ( 
        [CampaignId] => 4 
        [DID] => 2394434445 
        [FWDDID] => 3214822821 
        [ProductId] => 1 
        [CampaignName] => Cape Coral Billboard #2 
        [ProductName] => CallTrack - Sharktek 
        [Active] => 1 [CompanyId] => 1 ) 

    [2] => Array ( 
        [CampaignId] => 5 
        [DID] => 2392146000 
        [FWDDID] => 3214822821 
        [ProductId] => 1 
        [CampaignName] => Test Campaign 
        [ProductName] => CallTrack - Sharktek 
        [Active] => 1 
        [CompanyId] => 1 )
    ) 
)

我正在尝试创建一个foreach循环,它选择所有的CampaignIds并在每个循环中使用它们,遗憾的是,我无法这样做。

因为我正在使用CodeIgniter,所以我无法简单地在控制器中回显结果,而是我必须将我想要的值传递给另一个数组以发送到视图

感谢您的时间和努力,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如果您只想稍后将所有CampaignId传递给foreach并且您有PHP> = 5.5.0,那么请使用array_column

$ids = array_column($array, 'CampaignId');

如果PHP< 5.5.0然后foreach超过$array['campaigns']

foreach($array['campaigns'] as $campaign) {
    $ids[] = $campaign['CampaignId'];
}