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.

4
  • 3
    if you have PHP 5.5 or greater you can also use array_column() Commented Jan 5, 2015 at 7: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? :) Commented Jan 5, 2015 at 7:25
  • possible duplicate of Turning multidimensional array into one-dimensional array Commented Jan 5, 2015 at 7: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. Commented Jan 5, 2015 at 7:35

4 Answers 4

1

Try this -

$new = array();
foreach($yourArray as $value) {
    $new[] = $value['title'];
}
var_dump($new);
Sign up to request clarification or add additional context in comments.

Comments

1

Your code should be

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

Comments

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 )

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.