将数组合并到没有键的数组?

发布于 2024-11-16 16:22:42 字数 508 浏览 0 评论 0原文

我正在尝试让 Facebook 上传带有随机标签朋友的照片。 程序运行正确,但我在数组上得到了堆栈

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

$id 是数组好友列表,格式如下

$friends = $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;
}

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

$friends = $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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-11-23 16:22:42

更新2:

请阅读有关数组的内容:PHP 手册数组< /a>.即使您没有指定,数组中的每个元素都有一个键。数组中不能有没有键的元素。

定义数组(无需指定键)和打印数组(为您提供数组的文本表示形式)之间存在差异。

更新: 所以看来它必须是

$args['tags'] = $id;

$id 已经是一个数组了。如果将其传递给 array,它将创建一个以 $id 作为第一个元素的新数组。


旧答案:

您已经在谈论合并。您看过array_merge[文档]

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

当然, $args['tags'] = array( $id ); 不起作用。它

  1. 覆盖 $args['tags'] 已有的值。
  2. 正如您已经注意到的,它将已经是数组的 $id 添加到数组中。如果 $args['tags'] 没有值,您只需执行 $args['tags'] = $id; 即可。

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;.
小矜持 2024-11-23 16:22:42

我建议使用compact($example1,$example2,$example3),但结果将与您可能习惯的数组合并不同:

$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" } }

涵盖compact的php手册的链接是:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文