3

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.

4
  • Arrays will always have keys. But if you're trying to re-index the array with keys starting from 0, then use array_values. Commented Oct 19, 2013 at 12:20
  • it is generating keys, kindly check the example Commented Oct 19, 2013 at 12:21
  • You already have that array! All arrays have keys Commented Oct 19, 2013 at 12:21
  • is there no way to get array like this: array('one', 'two', 'three'); Commented Oct 19, 2013 at 12:22

2 Answers 2

8

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);
Sign up to request clarification or add additional context in comments.

Comments

0
/*
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.