-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.

4
  • what you tried so far... provide your tried code Commented Sep 7, 2016 at 6:00
  • I think if you take a gander at array functions in the PHP manual you will find what you are looking for. Commented Sep 7, 2016 at 6:00
  • Can use this stackoverflow.com/questions/6914105/… Commented Sep 7, 2016 at 6:05
  • I want comma separated value as : array(banana,apple,grapes,orange); I need this array to put this for generate CSV. Commented Sep 7, 2016 at 6:36

3 Answers 3

1

use array_values function:

$array = array_values($array);

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

You can use

$new_array = array_values($your_array);

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

2 Comments

I want comma separated value as : array(banana,apple,grapes,orange); I need this array to put this for generate CSV.
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
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
)

2 Comments

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.
@NehaPareek if you print array(banana,apple,grapes,orange); the output is same as mine . $new is same as array(banana,apple,grapes,orange);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.