开发者

merge array to array without key?

im trying to make facebook upload photo with random tag friend. the program was run correctly but i got stack on the array

$args = array(  
    'message' => 'Test Tags Text',  
    "access_token" => $access_token,  
    "image" => $files,
    'tags'    => $id,
); 

the $id is array friend list here the format

$frien开发者_开发技巧ds = $facebook->api('/me/friends'); 
$frand = array_rand( $friends['data'], 20 );

foreach($frand as $fid){ 
    $id[$fid]['tag_uid'] =  $friends['data'][$fid]['id'];
$id[$fid]['x']  = 0;
$id[$fid]['y']  = 0;
}


Update 2:

Please read about arrays: PHP manual arrays. Every element in an array has a key even if you don't specify it. You cannot have an element in an array without key.

There is a difference between defining an array, where you don't have to specify keys, and printing the array, which gives you a textual representation of the array.

Update: So it seems it has to be

$args['tags'] = $id;

$id is already an array. If you pass it to array, it will create a new array with the $id as first element.


Old answer:

You are already talking about merge. Have you had a look at array_merge[docs]?

$args['tags'] = array_merge($args['tags'], $id);

Of course, $args['tags'] = array( $id ); does not work. It

  1. Overwrites the already existing value of $args['tags'].
  2. As you already noticed, it adds $id which is already an array, to an array. If $args['tags'] does not have a value, you could just do $args['tags'] = $id;.


I would suggest using compact($example1,$example2,$example3), but the result will be different than what you might be used to with array merge:

$example1 = array (x,y,z);
$example2 = array (a,b,c);
$example3 = array (d,e,f);

$mergedValue = compact('example1','example2','example3');
var_dump ($mergedValue);

// output will be:
array(3) { ["example1"]=> array(3) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" } ["example2"]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" } ["example3"]=> array(3) { [0]=> string(1) "d" [1]=> string(1) "e" [2]=> string(1) "f" } }

The link to php manual where compact is covered is:

http://php.net/manual/en/function.compact.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜