0

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?

5
  • In this foreach ($targeting as $i => $value) { doit($accountid,$value); } how you are getting $accountid? Commented May 2, 2017 at 10:36
  • good question. its defined before the foreach. isn't it global then? I'll update the main post. Commented May 2, 2017 at 10:39
  • can you share your expected output? Commented May 2, 2017 at 10:45
  • output should be: ... [status] => PAUSED [targeting] => Array ([device_platforms] => Array([0] => desktop) [interests] => Array(Array([id]=> 435345 [name] => test)) Commented May 2, 2017 at 10:56
  • actually it's: [status] => PAUSED [targeting] => Array ([0] => 'device_platforms' => array('desktop'),'custom_audiences'=> ['id' => '435345'])... Commented May 2, 2017 at 11:04

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

9 Comments

looks much better, but i still have the leading [0] in the $fields. [0] => Array ( [device_platforms] => Array ( [0] => desktop ) [interests] => Array ( [0] => Array ( [id] =>.... should be [device_platforms] => Array ( [0] => desktop ) [interests] => Array ( [0] => Array ( [id] => ...
change "'device_platforms to 'device_platforms look at Sahils answer, he removed the doublequotes.
@Holger: Within the array definition. Can't find a " 'device_platforms where i can leave ". i've tried to change ' to ". makes no difference.
@swapfile In you comment you have mentioned output should be [status] => PAUSED [targeting] => Array ([device_platforms] => Array([0] => desktop) [interests] => Array(Array([id]=> 435345 [name] => test)) thats what i did
@swapfile Yes , thats the changes which i have done , in my post, i Hope it will be working fine now..If you have any doubts then go through the demo url which i have pasted in my post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.