将数组合并到没有键的数组?
我正在尝试让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新2:
请阅读有关数组的内容:PHP 手册数组< /a>.即使您没有指定,数组中的每个元素都有一个键。数组中不能有没有键的元素。
定义数组(无需指定键)和打印数组(为您提供数组的文本表示形式)之间存在差异。
更新: 所以看来它必须是
$id
已经是一个数组了。如果将其传递给array
,它将创建一个以$id
作为第一个元素的新数组。旧答案:
您已经在谈论合并。您看过
array_merge
[文档]?当然,
$args['tags'] = array( $id );
不起作用。它$args['tags']
已有的值。$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
$id
is already an array. If you pass it toarray
, 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]?Of course,
$args['tags'] = array( $id );
does not work. It$args['tags']
.$id
which is already an array, to an array. If$args['tags']
does not have a value, you could just do$args['tags'] = $id;
.我建议使用compact($example1,$example2,$example3),但结果将与您可能习惯的数组合并不同:
涵盖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:
The link to php manual where compact is covered is:
http://php.net/manual/en/function.compact.php