douyiken0968 2017-05-02 10:21
浏览 54
已采纳

在数组php foreach中插入变量

i have a function with a dynamic array.

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

And i want to build the array "$fields" dynamically within a foreach loop. Like that:

$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);
    }

The Problem is, that the array within the function will not be correctly filled. If i output the array in the function i get something like:

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

The beginning [0] should be the problem. Any ideas what im doing wrong?

  • 写回答

1条回答 默认 最新

  • dongtang5057 2017-05-02 11:05
    关注

    Hope this will help you out. The problem was the way you are defining $targeting array. You can't have multiple keys with same name

    Change 1:

    $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'))
        )
    );
    

    Change 2:

    $fields = array(
            'name' => "test",
            'status' => "PAUSED",
            'targeting' => $targeting //removed array
        );
    

    Try this code snippet here this will just print postfields

    <?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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?