简体   繁体   English

如何将数组的数组合并到一个PHP

[英]How to merge array of arrays into one php

I have the following array. 我有以下数组。

 Array
    (
        [0] => Array
            (
                [title] => IT Software - Application Programming, Maintenance 
            )

        [1] => Array
            (
                [title] => IT Software - eCommerce, Internet Technologies 
            )

        [2] => Array
            (
                [title] => IT Software - Client/ Server Programming
            )

        [3] => Array
            (
                [title] => IT Software - Other 
            )

    )

Would like to get the resultant array as below 想得到结果数组如下

Array
(
    [0] => IT Software - Application Programming, Maintenance

    [1] => IT Software - eCommerce, Internet Technologies 

    [2] => IT Software - Client/ Server Programming

    [3] => IT Software - Other 


)

can i get a simple one liner other than array_column() because im running php version below 5.5. 我能得到除array_column()以外的一个简单的班轮array_column()因为我运行的PHP版本低于5.5。 I tried $funcmerged = array_reduce($functionalres, 'array_merge', array()); 我尝试了$funcmerged = array_reduce($functionalres, 'array_merge', array()); but iam not getting desired result. 但我没有得到理想的结果。

Try this - 尝试这个 -

$new = array();
foreach($yourArray as $value) {
    $new[] = $value['title'];
}
var_dump($new);

Your code should be 您的代码应为

$newArr = array();
foreach($currentArr as $key=>$val){
    $newArr[] = $val['title'];
}
print_r($newArr);

Try this.. 尝试这个..

<?php
$newarray=array();
$array=array
(
    "0" => array("title"=>"IT Software - Application Programming, Maintenance"),
    "1"     => array("title"=>"IT Software - eCommerce, Internet Technologies   "),
    "2"    => array("title"=>"IT Software - Client/ Server Programming"),
    "3"    => array("title"=>"IT Software - Other")
); 

         foreach($array as $key =>$arrayvalue)
         {
         $newarray[]=$arrayvalue['title'];
         }
print_r($newarray);
?>

Result: 结果:

    Array ( [0] => IT Software - Application Programming, Maintenance 
[1] => IT Software - eCommerce, Internet Technologies   
[2] => IT Software - Client/ Server Programming 
[3] => IT Software - Other )

Found a pretty good solution 找到了一个很好的解决方案

How to Flatten a Multidimensional Array? 如何展平多维数组?

function flatten(array $array) {
        $return = array();
        array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
        return $return;
    }

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM