0

I want to convert an array value without using any loop and i have an array something like this

Array
  (
[0] => Array
       (
        [month] => August
       )

[1] => Array
    (
        [month] => July
     )

[2] => Array
    (
        [month] => October
    )
)

I want to convert above array in to below format

Array
(
    [0] => August
    [1] => July
    [2] => October
)

Without using any loop

Barmar
  • 741,623
  • 53
  • 500
  • 612
Sheenu
  • 31
  • 9

2 Answers2

1

If you're using PHP 5.5 or higher, you can use the array_column function:

$new_array = array_column($array, 'month');

In older versions of PHP, you can use array_map

$new_array = array_map(function($x) { return $x['month']; }, $array);

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

it's my code so plz change it.... and try this

 $a = array(0, 4, 5, 7);
// PHP 5.3+ anonmymous function.
$output = array_map(function($val) { return $val+1; }, $a);

print_r($output);
Array
(
    [0] => 1
    [1] => 5
    [2] => 6
    [3] => 8
)

Edit by OP:

function doubleValues($a) {
  return array_map(function($val) { return $val * 2; }, $a);
}
Nimesh Patel
  • 796
  • 1
  • 7
  • 23