合并PHP数组成简单阵列? [英] Merge PHP Arrays into Simple Array?
问题描述
我用的print_r($测试)
来显示这样的数组:
I use print_r($test)
to display arrays like this:
Array
(
[0] => 123.5
)
Array
(
[0] => 123.6
)
Array
(
[0] => 121.1
)
这个名单下降至少100阵列,有时多有时少。我想这些阵列转换成一个简单的数组是这样的:
This list goes down for at least 100 arrays, sometimes more and sometimes less. I want to convert these arrays into a simple array like this:
Array
(
[0] => 123.5
[1] => 123.6
[2] => 121.1
)
我怎样才能做到这一点?我试着用 array_merge()
,但它不工作。
修改
这是我如何得到我的数组:
This is how I get my arrays:
$i=0;
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj) {
$title = trim($linkObj->plaintext);
$test = array($title);
print_r($test);
$i++;
}
我只使用的号码作为一个例子;其实,我处理的文本。对不起,这个误导,我是新这里。
I only used the numbers as an example; I actually deal with text. Sorry for this mislead, I am new at this.
推荐答案
这应该为你工作:
array_reduce()
阵列为一维数组。
Just array_reduce()
your array into a one dimensional array.
$result = array_reduce($arr, "array_merge", [])
编辑:
要得到你的结果,你必须重写codeA位,让您收集的所有值到一个数组由去扔 $ linkObjs
中的所有元素与 array_map()
。
To get your result you have to rewrite your code a bit, so that you collect all values into one array by go throw all elements of $linkObjs
with array_map()
.
$linkObjs = $html->find('h3.r a');
$i = count($linkObjs);
$result = array_map(function($v){
return trim($v->plaintext);
}, $linkObjs);
print_r($result);
这篇关于合并PHP数组成简单阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!