我是PHP的新手,在下面的代码中,$ strng值是为数组动态创建的。
$strng='"Active Life" => "6","Arts & Entertainment" => "4","Beauty & Spa" => "3","Food & Drink" => "1","Hotels" => "10","Local Services" => "8","Nightlife" => "2","Pets" => "9","Public Services" => "7","Shopping" => "5"';
$bussCatry=array($strng);
foreach($bussCatry as $x=>$x_value)
{
error_log( "Key=" . $x . ", Value=" . $x_value);
}
但是当我试图创建数组时,我失败了,我得到了低于。 只有一个键值对
Key=0,
Value="Active Life" =>"6",
"Arts & Entertainment" =>"4",
"Beauty & Spa" =>"3",
"Food & Drink" =>"1",
"Hotels" =>"10",
"Local Services" =>"8",
"Nightlife" =>"2",
"Pets" =>"9",
"Public Services" =>"7",
"Shopping" =>"5"
答案 0 :(得分:0)
查看php的array语法。
试试这个:
$bussCatry = array(
'Active Life' => '6',
'Arts & Entertainment' => '4',
'Beauty & Spa' => '3',
'Food & Drink' => '1',
'Hotels' => '10',
'Local Services' => '8',
'Nightlife' => '2',
'Pets' => '9',
'Public Services' => '7',
'Shopping' => '5',
);
答案 1 :(得分:0)
eval("\$bussCatry = array({$strng});");
答案 2 :(得分:0)
创建一个直接赋值的数组。
$bussCatry = array(
"Active Life" => "6",
"Arts & Entertainment" => "4",
"Beauty & Spa" => "3",
"Food & Drink" => "1",
"Hotels" => "10",
"Local Services" => "8",
"Nightlife" => "2",
"Pets" => "9",
"Public Services" => "7",
"Shopping" => "5"
);
请勿将array values
转换为single quotes
的字符串。它不像array
那样解析。
答案 3 :(得分:0)
array()函数不解析字符串。
array("a" => "orange", "b" => "banana", "c" => "apple")
正如您在示例中所看到的,不仅有一个简单的字符串,还有参数。
答案 4 :(得分:0)
尝试使用此代码发布了错误的代码。
$strng=array("Active Life" => "6","Arts & Entertainment" => "4","Beauty & Spa" => "3","Food & Drink" => "1","Hotels" => "10","Local Services" => "8","Nightlife" => "2","Pets" => "9","Public Services" => "7","Shopping" => "5");
//$bussCatry=array($strng);
foreach($strng as $x=>$x_value)
{
echo( "Key=" . $x . ", Value=" . $x_value);
}
让我知道它是否适合你:)
感谢。
答案 5 :(得分:0)
查看token_get_all,您可以将字符串解析为标记,然后根据需要构建数组。对不起,它有点乱,但它是一个不错的工作启动器。
<?php
define('STATE_WAITING_FOR_ARRAY', 1);
define('STATE_WAITING_FOR_KEY', 2);
define('STATE_WAITING_FOR_VALUE', 3);
$string = '"Active Life" => "6","Arts & Entertainment" => "4","Beauty & Spa" => "3","Food & Drink" => "1","Hotels" => "10","Local Services" => "8","Nightlife" => "2","Pets" => "9","Public Services" => "7","Shopping" => "5"';
$string = sprintf('<?php array(%s); ?>', $string);
$tokens = token_get_all($string);
$state = STATE_WAITING_FOR_ARRAY;
$array = array();
foreach($tokens as $token) {
if( ! is_array($token)) {
continue;
}
$name = token_name($token[0]);
$value = $token[1];
if(STATE_WAITING_FOR_ARRAY === $state && 'T_ARRAY' === $name) {
$state = STATE_WAITING_FOR_KEY;
continue;
}
if(STATE_WAITING_FOR_KEY === $state && 'T_CONSTANT_ENCAPSED_STRING' === $name) {
$state = STATE_WAITING_FOR_VALUE;
$key = $value;
continue;
}
if(STATE_WAITING_FOR_VALUE === $state && 'T_CONSTANT_ENCAPSED_STRING' === $name) {
$state = STATE_WAITING_FOR_KEY;
$array[$key] = $value;
$key = null;
continue;
}
}
var_dump($array);
/*
array(10) {
[""Active Life""]=>
string(3) ""6""
[""Arts & Entertainment""]=>
string(3) ""4""
[""Beauty & Spa""]=>
string(3) ""3""
[""Food & Drink""]=>
string(3) ""1""
[""Hotels""]=>
string(4) ""10""
[""Local Services""]=>
string(3) ""8""
[""Nightlife""]=>
string(3) ""2""
[""Pets""]=>
string(3) ""9""
[""Public Services""]=>
string(3) ""7""
[""Shopping""]=>
string(3) ""5""
}
*/