0

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. I tried $funcmerged = array_reduce($functionalres, 'array_merge', array()); but iam not getting desired result.

Amit
  • 3,251
  • 3
  • 20
  • 31
  • 3
    if you have PHP 5.5 or greater you can also use [`array_column()`](http://php.net/manual/en/function.array-column.php) – Kevin Jan 05 '15 at 07:24
  • Rather than "fixing" this one, perhaps it would be better to create the array in the first place to not contain a subarray for each element in the first place? :) – Fluffeh Jan 05 '15 at 07:25
  • possible duplicate of [Turning multidimensional array into one-dimensional array](http://stackoverflow.com/questions/8611313/turning-multidimensional-array-into-one-dimensional-array) – Naing Lin Aung Jan 05 '15 at 07:25
  • can i get a simple one liner other than array_column() because im running php version below 5.5. I tried $funcmerged = array_reduce($functionalres, 'array_merge', array()); but iam not getting desired result. – Amit Jan 05 '15 at 07:35

4 Answers4

1

Try this -

$new = array();
foreach($yourArray as $value) {
    $new[] = $value['title'];
}
var_dump($new);
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

Your code should be

$newArr = array();
foreach($currentArr as $key=>$val){
    $newArr[] = $val['title'];
}
print_r($newArr);
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
0

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 )
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

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.

Community
  • 1
  • 1
Amit
  • 3,251
  • 3
  • 20
  • 31