$a = array('matches' =>
array(
'5' => array('weight' => 6),
'15' => array('weight' => 6),
)
);
$b = array('matches' =>
array(
'25' => array('weight' => 6),
'35' => array('weight' => 6),
)
);
$merge = array_merge($a, $b);
print_r($merge);
Run Code Online (Sandbox Code Playgroud)
这个脚本的结果是
Array
(
[matches] => Array
(
[25] => Array
(
[weight] => 6
)
[35] => Array
(
[weight] => 6
)
)
)
Run Code Online (Sandbox Code Playgroud)
但为什么?
我想结果是这样的:
Array
(
[matches] => Array
(
[5] => Array
(
[weight] => 6
) …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 PHP 数组:
$array['my_data']['value'] = 'some value';
$array['my_own_data']['value'] = 'another value';
$array['different_data']['value'] = 'another value';
Run Code Online (Sandbox Code Playgroud)
我需要在 PHP 等中使用 array_merge。问题是第一级密钥的数量是未知的。可能是 150 个项目,我不知道。
由于未知数量的键,这对我不起作用:
array_merge($array['my_data'], $array['my_own_data'], $array['different_data']);
Run Code Online (Sandbox Code Playgroud)
我需要一个循环还是有什么花哨的东西?
数组1:
Array (
'127.0.0.1',
'235.107.12.3'
)
Run Code Online (Sandbox Code Playgroud)
数组2:
Array (
'34.235.54.6',
'230.56.78.1'
)
Run Code Online (Sandbox Code Playgroud)
最终阵列应如下所示:
Array (
[127.0.0.1] => Array (
'34.235.54.6',
'230.56.78.1'
),
[235.107.12.3]' => Array (
'34.235.54.6',
'230.56.78.1'
)
)
Run Code Online (Sandbox Code Playgroud)
请提供一个关于如何合并这两个数组(数组1和数组2)以获得所需结果的建议.
我有一个看起来像这样的数组:
array(2) {
[0]=> array(2) {
[0]=> string(52) "./app/pictures/uploads/Audi/A1/name1.jpg"
[1]=> string(52) "./app/pictures/uploads/Audi/A1/name2.jpg"
}
[1]=> array(1) {
[0]=> string(52) "./app/pictures/uploads/Audi/A3/name3.jpg"
}
}
Run Code Online (Sandbox Code Playgroud)
上面的数组可以有两个以上的键(0,1).更多信息,是我浏览文件夹.如果有子文件夹,它会将每个子文件夹放在一个数组中,并将这些子文件夹的内容/文件放在该数组中.
所以对于我的结果我需要这样的东西:
array(3) {
[0]=> string(52) "./app/pictures/uploads/Audi/A1/name1.jpg"
[1]=> string(52) "./app/pictures/uploads/Audi/A1/name2.jpg"
[2]=> string(52) "./app/pictures/uploads/Audi/A3/name3.jpg"
}
Run Code Online (Sandbox Code Playgroud)
我意识到array_merge:
$array = array_merge($tmparray[0],$tmparray[1]);
Run Code Online (Sandbox Code Playgroud)
现在你可以看到这里的键是固定的.但它们应该是动态的.我怎么能意识到这一点?也许是循环,但我没有得到线索,$array每次在该循环中都不会覆盖变量...
也许现在对此有一个清醒的想法为时已晚,但我很快就需要一个解决方案.
我有一个数组:
{
"zone1":{
"foo1":"bar1",
"foo2":"bar2",
"foo3":"bar3",
"foo4":"bar4"
},
"zone2":{
"newfoo1":"newbar1",
"newfoo2":"newbar2",
"newfoo3":"newbar3",
"newfoo4":"newbar4"
},
"zone3":{
"morefoo1":"morebar1",
"morefoo2":"morebar2",
"morefoo3":"morebar3",
"morefoo4":"morebar4"
}
}
Run Code Online (Sandbox Code Playgroud)
我想将第二个数组与更新后的值合并:
{
"zone1":{
"foo1":"updatedbar1"
},
"zone3":{
"morefoo2":"updatedbar2",
"morefoo4":"updatedbar4"
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多东西,我现在使用的是这个php代码:
$array3 = array_merge($array1, $array2);
Run Code Online (Sandbox Code Playgroud)
但是这段代码给了我这个:
{
"zone1":{
"foo1":"updatedbar1"
},
"zone2":{
"newfoo1":"newbar1",
"newfoo2":"newbar2",
"newfoo3":"newbar3",
"newfoo4":"newbar4"
},
"zone3":{
"morefoo2":"updatedbar2",
"morefoo4":"updatedbar4"
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的只是用第二个数组上的值更新第一个数组,而不丢失任何数据。数组是json,它们来自json文件,但语言是PHP。
我有两个数组,并寻找合并它们的方法.标准array_merge()功能不起作用.
如果没有foreach迭代,你知道任何好的解决方案吗?
我的第一个阵列:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] =>
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] =>
)
)
Run Code Online (Sandbox Code Playgroud)
我的第二个阵列:
Array
(
[0] => 2
[1] => 7
)
Run Code Online (Sandbox Code Playgroud)
结果我想得到:*
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
Run Code Online (Sandbox Code Playgroud) 我想在python 2.7中使用' for loop ' 合并两个数组:
from array import *
ary_1 = array ('i',[11,12,13])
ary_2 = array ('i',[14,15,16])
ary_3 = array ('i')
Run Code Online (Sandbox Code Playgroud)
应该在ary_3上给出输出,所以ary_3将按特定顺序显示如下:
ary_3 = array ('i',[11,12,13,14,15,16])
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码:
from array import *
ary_1 = array ('i',[11,12,13])
ary_2 = array ('i',[14,15,16])
ary_3 = array ('i')
ary_len = len (ary_1) + len (ary_2)
for i in range (0,ary_len):
ary_3.append (ary_1 [i])
ary_3.append (ary_2 [i])
if len (ary_3) == len (ary_1) + len (ary_2):
print ary_3,
break
Run Code Online (Sandbox Code Playgroud)
然后输出是:
array('i',[11,14,12,15,13,16]) …Run Code Online (Sandbox Code Playgroud) 我有这两个数组:
$list = [
'fruit' => [],
'animals' => [],
'objects' => [],
];
$dataArray = [
'fruit' => 'apple',
'animals' => ['dog', 'cat'],
'asd' => 'bla'
];
Run Code Online (Sandbox Code Playgroud)
我要合并它们,以便最后的$ list是:
[fruit] => Array
(
[0] => apple
)
[animals] => Array
(
[0] => dog
[1] => cat
)
[objects] => Array
(
)
Run Code Online (Sandbox Code Playgroud)
因此,注意事项:
使用array_merge不起作用:
$merged = array_merge($list, $dataArray);
[fruit] => apple
[animals] => Array
(
[0] => dog
[1] => cat
) …Run Code Online (Sandbox Code Playgroud) 我不能再做 php artisan serve 了,它说:
在 ServiceProvider.php 第 59 行:
Run Code Online (Sandbox Code Playgroud)array_merge(): Argument #2 is not an array
第 59 行代码在 ServiceProvider.php 中:
$this->app['config']->set($key, array_merge(require $path, $config));
Run Code Online (Sandbox Code Playgroud)
我不明白我的 ServiceProvider.php 有什么问题,我没有改变那里的任何东西。
我希望有人能帮助我。
我正在尝试使用key:[value array]动态构建对象,但是使用不同的方法,我总是以value数组中的单个项目(在响应中有多个值)结束。
伪代码:
var myFunction = function () {
var myObject = {};
$.ajax('http://the.url.com', {
type: "GET",
success: function (res) {
$(res).each(function (i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
myObject[name] = new Array();
myObject[name].push(id);
});
},
error: function (xhr) {}
}
}
Run Code Online (Sandbox Code Playgroud)
电流输出:
{
key1: ["value1c"]
key2: ["value2a"]
key3: ["value3b"]
}
Run Code Online (Sandbox Code Playgroud)
所需的输出:
{
key1: ["value1a", "value1b","value1c"]
key2: ["value2a"]
key3: ["value3a", "value3b"]
}
Run Code Online (Sandbox Code Playgroud) array-merge ×10
php ×8
arrays ×6
directory ×1
for-loop ×1
javascript ×1
laravel ×1
laravel-5 ×1
object ×1
python ×1
python-2.7 ×1
web-crawler ×1