开发者

convert array php

开发者 https://www.devze.com 2023-01-14 11:41 出处:网络
I want to convert this array content: $colorList[0] = \"red\"; $colorList[1] = \"green\"; $colorList[2] = \"blue\";

I want to convert this array content:

$colorList[0] = "red";
$colorList[1] = "green";
$colorList[2] = "blue";
$colorList[3] = "black";
$colorList[4]开发者_StackOverflow社区 = "white";

into

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

how do doing that? thanks


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',
)


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


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', ...)"


There is no need to convert.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消