user007
user007

Reputation: 3243

php - Indexed arrays without key

I want to get array without key,

Like current array is:

array([1]=>'one', [2]=>'two', [3]=>'three');

but I need this array without key, like this:

array('one', 'two', 'three');

please tell me how do I do that, I need to use this array as argument of a function, so it must have no keys.

Upvotes: 3

Views: 12072

Answers (2)

JOB
JOB

Reputation: 85

/*
Your array N/B make sure it is a legal array. So for it to be an array and 
produce this example  : array([1]=>'one', [2]=>'two', [3]=>'three');
We use the same of array with no index, since every array comes indexed from 0.
 */

$array          = array('one','two','three');

//final array variable 
$finalArray     = array();

//encode the arry to Json
$jsonArray      = json_encode($array);
//first search & replace
$firstReplace   = str_replace('[','array(',$jsonArray);
//last search & replace
$finalArray     = str_replace(']',')',$firstReplace);
//output
print_r($finalArray);

Upvotes: 0

rpkamp
rpkamp

Reputation: 811

Arrays always have keys, whether you want them or not. Even a simple array('one', 'two', 'three'); will be array([0] => 'one', [1] => 'two', [2] => 'three');. That being said, your array does start with 1, and not with 0 (which is what want, I guess). To get the array starting at 0 you can use the array_values function:

$new_array = array_values($old_array);

Upvotes: 8

Related Questions