将PHP数组合并为简单数组?

时间:2021-07-05 12:18:06

I use print_r($test) to display arrays like this:

我使用print_r($ test)来显示这样的数组:

Array
(
    [0] => 123.5
)
Array
(
    [0] => 123.6
)
Array
(
    [0] => 121.1
)

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:

此列表至少有100个阵列,有时更多,有时更少。我想将这些数组转换为这样的简单数组:

Array
(
    [0] => 123.5
    [1] => 123.6
    [2] => 121.1
)

How can I achieve this? I tried using array_merge(), but it does not work.

我怎样才能做到这一点?我尝试使用array_merge(),但它不起作用。

EDIT

编辑

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.

我只用数字作为例子;我实际上处理文本。抱歉这个误导,我是新来的。

4 个解决方案

#1


3  

This should work for you:

这应该适合你:

Just array_reduce() your array into a one dimensional array.

只需将array_reduce()数组转换为一维数组。

$result = array_reduce($arr, "array_merge", [])

EDIT:

编辑:

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().

要获得结果,您必须稍微重写代码,以便通过使用array_map()抛出$ linkObjs的所有元素来将所有值收集到一个数组中。

$linkObjs = $html->find('h3.r a'); 
$i = count($linkObjs);
$result = array_map(function($v){
    return trim($v->plaintext);
}, $linkObjs);

print_r($result);

#2


3  

You can use the function array_column for this, it returns only one column of the array.

您可以使用函数array_column,它只返回数组的一列。

$merged = array_column($array, 0);

#3


2  

Use this code:-

使用此代码: -

<?php 
$i=0;
$data=array();
$linkObjs = $html->find('h3.r a'); 
foreach ($linkObjs as $linkObj) {

    $title = trim($linkObj->plaintext);
    $data[]=title;// this line is main line ---
    $test = array($title);
    print_r($test);
    $i++;

}
print_r($data);
?>

#4


1  

You can use any of following...

您可以使用以下任何一种......

$merged = array_column($test, 0);

OR

$merged = array();

foreach($test as $t){
    $merged[] = $t[0];
}

print_r($merged);

#1


3  

This should work for you:

这应该适合你:

Just array_reduce() your array into a one dimensional array.

只需将array_reduce()数组转换为一维数组。

$result = array_reduce($arr, "array_merge", [])

EDIT:

编辑:

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().

要获得结果,您必须稍微重写代码,以便通过使用array_map()抛出$ linkObjs的所有元素来将所有值收集到一个数组中。

$linkObjs = $html->find('h3.r a'); 
$i = count($linkObjs);
$result = array_map(function($v){
    return trim($v->plaintext);
}, $linkObjs);

print_r($result);

#2


3  

You can use the function array_column for this, it returns only one column of the array.

您可以使用函数array_column,它只返回数组的一列。

$merged = array_column($array, 0);

#3


2  

Use this code:-

使用此代码: -

<?php 
$i=0;
$data=array();
$linkObjs = $html->find('h3.r a'); 
foreach ($linkObjs as $linkObj) {

    $title = trim($linkObj->plaintext);
    $data[]=title;// this line is main line ---
    $test = array($title);
    print_r($test);
    $i++;

}
print_r($data);
?>

#4


1  

You can use any of following...

您可以使用以下任何一种......

$merged = array_column($test, 0);

OR

$merged = array();

foreach($test as $t){
    $merged[] = $t[0];
}

print_r($merged);