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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题