0

How can i convert an array like

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
    [5] => F
)

to

Array
    (
        A
        B
        C
        D
        E
        F
    )

I tried using foreach key value and assigning value to new array , but even new array will be in 2D form

3
  • 3
    This question makes no sense. You'll always have keys for your array. Commented Mar 28, 2012 at 8:05
  • 1
    The basic mistake here is assuming this array as 2-dimensional. Commented Mar 28, 2012 at 8:06
  • The closest thing to what you want would be a String, I guess. So just implode('', $array) the array. Which you could still access with $string[0] => A, though. Commented Mar 28, 2012 at 8:09

5 Answers 5

3

This is not a 2d array to start with. It is a 1d array... all arrays in PHP comprise key/value pairs. so

array('A','B') 

is a 1d array with values A and B, to which keys are automatically assigned (0 and 1) as their offset position in that array.

A 2d array would be something like

array( array('A','B')
       array('C','D')
     ) 
Sign up to request clarification or add additional context in comments.

4 Comments

The [0],[1], ... shown are keys or indexes, not values. They are in the same first dimension. ideone.com/5eVJM1
@PaulH - Do I say anywhere in this answer that 0 and 1 are values? I specifically say that they are keys: to which keys are automatically assigned (0 and 1)
What you say is completely right. I gave it a +1. I just wanted to add some information for a novice, like GoodSp33d back in 2012
@PaulH Lol Cant believe I asked this question. I cannot delete it also :D
2

You should probably look into what 2D arrays are:

1D array:

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
    [5] => F
)

1D associative array:

Array
(
    [foo] => A
    [bar] => B
    [moreFoo] => C
    [someElse] => D
)

2D array:

Array
(
    [0] => Array
    (
        [0] => A
        [1] => B
        [2] => C
        [3] => D
        [4] => E
        [5] => F
    )
    [1] => Array
    (
        [0] => A
        [1] => B
        [2] => C
        [3] => D
        [4] => E
        [5] => F
    )
    [2] => Array
    (
        [0] => A
        [1] => B
        [2] => C
        [3] => D
        [4] => E
        [5] => F
    )
)

2D associative array:

Array
(
    [foo] => Array
    (
        [foo] => A
        [bar] => B
        [moreFoo] => C
        [someElse] => D
    )
    [name] => Array
    (
        [foo] => A
        [bar] => B
        [moreFoo] => C
        [someElse] => D
    )
    [thirdElement] => Array
    (
        [foo] => A
        [bar] => B
        [moreFoo] => C
        [someElse] => D
    )
)

1 Comment

There is not actual php code answer here, just an explanation of the various array types.
0

Your question doesn't make sense... If you have that "flat" array, how are you gonna address, for example the 3rd element of it?

Also as @Shiplu mentioned in the comment, the array you use as an example is already a 1-d array, 2-d's array is in the form of

$array['level1']['level2'] = 'value';

Comments

0

All arrays in php have keys, whether you assign them or not when you create the array. The index starts at 0, so you would access A for example using 0, B using 1 etc.

Comments

0

If I correctly understand your question, you want to convert an unsorted indexed array something similar to:

array (
    [2] => A,
    [5] => B,
    [8] => C,
    ["x"] => D,
    ["v"] => E,
    [0] => F
);

to

array (
    A,
    B,
    C,
    D,
    E,
    F
);

If it is the case then use the array_values function.

$arr = array_values($arr);

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.