-1

I am having an array like :

array
(
  ['fruit1'] => banana,
  ['fruit2'] => apple,
  ['fruit3'] => grapes,
  ['fruit4'] => orange 
)

And I want the array Like :

array(banana,apple,grapes,orange);

Please suggest how can I convert it.

Yogita
  • 170
  • 9

3 Answers3

1

use array_values function:

$array = array_values($array);

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33
1

You can use

$new_array = array_values($your_array);

Note: You dont need commas after banana, apple and grapes in the main array.

masterFly
  • 1,072
  • 12
  • 24
  • I want comma separated value as : array(banana,apple,grapes,orange); I need this array to put this for generate CSV. – Yogita Sep 07 '16 at 06:34
  • You cant put an array as a line in a csv. what you can do is make a `STRING` using your array. `$string = implode(", ", array_values($your_array));` The above should work – masterFly Sep 07 '16 at 07:34
0

Try this

<?php 
echo "<pre>";
$arr = array('fruit1'=>'banana','fruit2'=>'apple','fruit3'=>'grapes','fruit4'=>'orange');
$new = array_values($arr);
print_r($new);

For more info about array_values please read http://php.net/manual/en/function.array-values.php

Output

Array
(
    [0] => banana
    [1] => apple
    [2] => grapes
    [3] => orange
)
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
  • I dont want these indexes. I want comma separated value as : array(banana,apple,grapes,orange); I need this array to put this for generate CSV. – Yogita Sep 07 '16 at 06:36
  • @NehaPareek if you print `array(banana,apple,grapes,orange);` the output is same as mine . `$new` is same as `array(banana,apple,grapes,orange);` – Passionate Coder Sep 07 '16 at 07:27