tags:

views:

44

answers:

4
+1  Q: 

convert array php

I want to convert this array content:

$colorList[0] = "red";
$colorList[1] = "green";
$colorList[2] = "blue";
$colorList[3] = "black";
$colorList[4] = "white";

into

array("red","green","blue","black","white")

how do doing that? thanks

+1  A: 

You don't need it.
there is no difference between these two.
What made you think you need that conversion?

Col. Shrapnel
yes, for now its working.maybe I made mistake before.thanks
andesign
A: 

There is no need to convert.

$colorList is same as array("red","green","blue","black","white")

Do var_dump($colorList); just do convince yourself.

codaddict
+1  A: 

Although there's probably a better way to achieve what you're looking for, take a look at var_export.

echo var_export($colorList, true); // "array('red', 'green', ...)"
Casey Hope
+2  A: 

If your array $colorList does not contain other elements, then both arrays already are equivalent:

$a = array();
$a[0] = "red";
$a[1] = "green";
$a[2] = "blue";
$a[3] = "black";
$a[4] = "white";
$b = array("red","green","blue","black","white");

var_dump($a === $b); // bool(true)

They are just created in a different way.

And if you just want to get an expression that represents these arrays, you can use var_export to get an output like this:

array (
  0 => 'red',
  1 => 'green',
  2 => 'blue',
  3 => 'black',
  4 => 'white',
)
Gumbo