我在一个php应用程序中工作,其应用程序的类和命名空间导致了
的问题 $array=array_merge($a,$b)
给了我
Array
(
[0] => Array
(
[blue shoes] => 1464873
[white shoes] => 2079
[red shoes] => 0.1419
[pink shoes] => 115
)
[1] => Array
(
[black dress shoes] => 527471
[white dress shoes] => 42.5232
)
)
*****从那个数组开始我究竟能得到什么*****
Array
(
[0] => Array
(
[blue shoes] => 1464873
[white shoes] => 2079
[red shoes] => 0.1419
[pink shoes] => 115
[black dress shoes] => 527471
[white dress shoes] => 42.5232
)
)
答案 0 :(得分:1)
最简单的解决方法是将$array=array_merge($a,$b)
更改为:
$array = array_merge(array_pop($a), array_pop($b));
但是,如果你必须使用你提供的数组,你可以这样做:
$array = array_reduce($array, 'array_merge', []);
答案 1 :(得分:0)
看起来您的变量$a
和$b
不包含平面数组,而是包含单个元素的数组,每个元素都包含一个平面数组。所以你想合并这两个变量的两个第一个元素,我想说。
看一下这个例子:
<?php
$a = [
[
'blue shoes' => 1464873,
'white shoes' => 2079,
'red shoes' => 0.1419,
'pink shoes' => 115
]
];
$b = [
[
'black dress shoes' => 527471,
'white dress shoes' => 42.5232
]
];
print_r(array_merge($a, $b));
print_r(array_merge($a[0], $b[0]));
上述代码的输出显然是:
Array
(
[0] => Array
(
[blue shoes] => 1464873
[white shoes] => 2079
[red shoes] => 0.1419
[pink shoes] => 115
)
[1] => Array
(
[black dress shoes] => 527471
[white dress shoes] => 42.5232
)
)
Array
(
[blue shoes] => 1464873
[white shoes] => 2079
[red shoes] => 0.1419
[pink shoes] => 115
[black dress shoes] => 527471
[white dress shoes] => 42.5232
)
这表明了您面临的问题:
答案 2 :(得分:0)
也许您只是在寻找array_replace_recursive
- 但这取决于您的输入数组。
$a = [[
'blue shoes' => 1464873,
'white shoes' => 2079,
'red shoes' => 0.1419,
'pink shoes' => 115
]];
$b = [[
'black dress shoes' => 527471,
'white dress shoes' => 42.5232
]];
$c = array_replace_recursive($a, $b);
print_r($c);
那会打印
Array (
[0] => Array
(
[blue shoes] => 1464873
[white shoes] => 2079
[red shoes] => 0.1419
[pink shoes] => 115
[black dress shoes] => 527471
[white dress shoes] => 42.5232
)
)
答案 3 :(得分:0)
您的数组是多维的,包含数组#include <sstream>
#include <vector>
#include <string>
#include <iostream>
int main()
{
std::string test = "Saloon 4 0 0 50 0 50 100 0 100";
std::string name;
int num;
std::istringstream iss(test);
iss >> name;
iss >> num;
std::cout << "Name is " << name << ". Value is " << num << "\n";
std::vector<int> values;
int oneValue;
while (iss >> oneValue )
values.push_back(oneValue);
std::cout << "The values in vector are:\n";
for(auto& v : values)
std::cout << v << " ";
}
和数组0
。您可以迭代该数组并将其收集到一个新数组中。
1
结果:
$a = array(
array("blue shoes" => 1464873, "white shoes" => 2079, "red shoes" => 0.1419, "pink shoes" => 115),
array("black dress shoes" => 527471, "white dress shoes" => 42.5232)
);
$newarray = array();
foreach ($a as $key => $oldarray) {
foreach ($oldarray as $shoe => $indexcount) {
$newarray[$shoe] = $indexcount;
}
}
print_r($newarray);