在php foreach数组中插入变量

在php foreach数组中插入变量,php,arrays,Php,Arrays,我有一个带有动态数组的函数 function doIt($accountid,$targeting){ $post_url= "https://url".$accountid."/"; $fields = array( 'name' => "test", 'status'=> "PAUSED", 'targeting' => array( $targeting

我有一个带有动态数组的函数

function doIt($accountid,$targeting){
    $post_url= "https://url".$accountid."/";
    $fields = array(
          'name' => "test",
          'status'=> "PAUSED",
          'targeting' => array(
            $targeting
          ),
      ); 
   $curlreturn=curl($post_url,$fields);
};
我想在foreach循环中动态构建数组$fields。就像这样:

$accountid="57865";    
$targeting=array(
                      "'device_platforms' => array('desktop'),'interests' => array(array('id' => '435345','name' => 'test')),",
                      "'device_platforms' => array('mobile'), 'interests' => array(array('id' => '345345','name' => 'test2')),",
                    );

foreach ($targeting as $i => $value) {
        doit($accountid,$value);
    }
问题是,函数中的数组将无法正确填充。如果我在函数中输出数组,我会得到如下结果:

....[0] => array('device_platforms' => array('desktop'),'custom_audiences'=> ['id' => '356346']), ) 

开头[0]应该是问题所在。你知道我做错了什么吗

希望这能帮到你。问题在于定义$targeting数组的方式。不能有多个同名键

更改1:

改变2:

这将只打印postfields


在这个foreach$targeting为$i=>$value{doit$accountid,$value;}您是如何获得$accountid的?好问题。它是在foreach之前定义的。那不是全球性的吗?我将更新主要帖子。你能分享你的预期输出吗?输出应该是:。。。[状态]=>暂停[定位]=>阵列[设备平台]=>阵列[0]=>桌面[兴趣]=>ArrayArray[id]=>435345[名称]=>测试实际上是:[状态]=>暂停[定位]=>阵列[0]=>“设备平台”=>阵列“桌面”,“自定义用户”=>['id'=>'435345']…看起来好多了,但我在$fields中仍然领先[0]。[0]=>Array[device\u platforms]=>Array[0]=>desktop[interests]=>Array[0]=>Array[id]=>。。。。应该是[device\u Platform]=>Array[0]=>desktop[interests]=>Array[0]=>Array[id]=>…将“device\u Platform”更改为“device\u Platform查看Sahils答案”,他删除了双引号。@Holger:在数组定义中。找不到可以离开的“设备”平台。我试着换成。没有区别。@swapfile在你的评论中你提到的输出应该是[status]=>PAUSED[targeting]=>Array[device\u platforms]=>Array[0]=>desktop[interests]=>ArrayArray[id]=>435345[name]=>test这就是我想要的did@swapfile是的,这就是我在我的帖子中所做的改变,我希望它现在可以正常工作。如果你有任何疑问,请浏览我贴在帖子中的演示url
$targeting = array(
array(
    'device_platforms' => array('desktop'),
    'interests' => array(
        array('id' => '435345', 
            'name' => 'test')),
    ),
array(
    'device_platforms' => array('mobile'),
    'interests' => array(
        array('id' => '345345', 
            'name' => 'test2'))
    )
);
$fields = array(
        'name' => "test",
        'status' => "PAUSED",
        'targeting' => $targeting //removed array
    );
<?php

ini_set('display_errors', 1);

function doIt($accountid, $targeting)
{
    $post_url = "https://url" . $accountid . "/";
    $fields = array(
        'name' => "test",
        'status' => "PAUSED",
        'targeting' => $targeting
    );
    print_r($fields);
}

$accountid = "57865";
$targeting = array(
    array(
        'device_platforms' => array('desktop'),
        'interests' => array(
            array('id' => '435345', 
                'name' => 'test')),
        ),
    array(
        'device_platforms' => array('mobile'),
        'interests' => array(
            array('id' => '345345', 
                'name' => 'test2'))
        )
);
foreach ($targeting as $i => $value)
{
    doit($accountid, $value);
}