将数组合并到没有键的数组? [英] merge array to array without key?
问题描述
我试图让Facebook上传照片与随机标签的朋友。
程序运行正常但我在数组上堆栈
$ args = array(
'消息'=>'测试标签文本',
access_token=> $ access_token,
image=> $ files,
'tags'=&
);
$ 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;
}
更新2:
请阅读关于数组: PHP手动数组。数组中的每个元素都有一个键,即使你没有指定它。您不能在没有键的数组中有元素。
定义数组的区别在于您不必指定键和打印数组,它为您提供了数组的文本表示。
更新:似乎必须是
$ args ['tags'] = $ id;
$ id
已经是一个数组。如果将其传递给数组
,它将创建一个新数组,其中 $ id
作为第一个元素。 >
旧答案:
你是已经在谈论合并。你看过 array_merge
[docs] ?
$ args ['tags'] = array_merge($ args ['tags'],$ id);
当然, $ args ['tags'] = array($ id);
不起作用
- 覆盖已经存在的
$ args ['tags']
的值。 - 正如您已经注意到的,它将一个已经是数组的
$ id
添加到数组中。如果$ args ['tags']
没有值,你可以做$ args ['tags'] = $ id; code>。
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;
}
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
- Overwrites the already existing value of
$args['tags']
. - 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;
.
这篇关于将数组合并到没有键的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!