Cross Vander
Cross Vander

Reputation: 2177

Get array number from Array with String value

I have an array with string value in PHP for example : arr['apple'], arr['banana'], and many more -about 20-30 data (get it from some process). Now I want to get its value and return it to one variable.

For example, I have Original array is like this:

$arr['Apple']
$arr['Banana']
and more..

and result that I want is like this:

$arr[0] = "Apple"
$arr[1] = "Banana"
and more..

Any idea how to do that?

Upvotes: 1

Views: 47

Answers (4)

Use array_flip()

$new_arr = array_flip($old_arr);

Demonstration

Upvotes: 1

Dariush Jafari
Dariush Jafari

Reputation: 5443

use array_keys function:

$keys = array_keys($arr);

It returns an array of all the keys in array.

Upvotes: 0

Jason OOO
Jason OOO

Reputation: 3552

Why not using array_keys()?

$new_array = array_keys($array);

Upvotes: 3

Anri
Anri

Reputation: 1693

use foreach loop

foreach($arr as $key => $val){
    $new_var[] = $key; 
}

Upvotes: 0

Related Questions