我不知道如何有效地将这个数组的值从字符串转换为数组。我真的很感激任何建议。
array(6) {
["A"]=>
string(31) "['B' => 3, 'C' => 5, 'D' => 9],"
["B"]=>
string(41) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],"
["C"]=>
string(51) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],"
["D"]=>
string(51) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],"
["E"]=>
string(41) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],"
["F"]=>
string(31) "['C' => 3, 'D' => 2, 'E' => 5],"
}
所需输出:
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
似乎您正在尝试将数组字符串转换为数组。
您可以通过循环重复或使用函数来获得所需的输出。
我正在使用 正则表达式 和 preg_match_all
代码
$rawArray = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach($rawArray as $k => $v){
preg_match_all("/\'(.)\'/", $v, $key);
preg_match_all("/=> (\d)/", $v, $val);
$graph[$k] = array_combine($key[1], $val[1]);
}
print_r($graph);
输出
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
现场演示
说明:
$rawArray是关联数组,它的每个元素都包含类似于php数组的字符串。
我们循环遍历数组并使用 preg_match_all 将该字符串转换为数组并构建 $graph 多维数组。
当循环第一次执行时
$k
等于A
且$v
等于['B' => 3, 'C' => 5, 'D' => 9],
首先preg_match_all从$v(
['B' => 3, 'C' => 5, 'D' => 9],
)创建键数组,并将其分配给$key[1]
。现在 $key[1]
是数组 ['B', 'C', 'D']
。第二个 preg_match_all 从 $v (['B' => 3, 'C' => 5, 'D' => 9],) 创建值数组,并将其分配给
$val[1]
。现在 $val[1]
是数组 [2, 5, 9]
。我们通过使用
array_combine将
$key[1]
组合为键,将 $val[1]
组合为值,其中 $graph[$k]
是 $k
。
A
它匹配字符串中的模式,然后将结果作为数组分配给
preg_match_all($pattern, $string, $out);
。
了解更多。 注意:
我们使用非捕获模式,因此它返回精确匹配和所需匹配...因此我们所需的记录在$out
中找到。
$key[1]
<?php
$graph = array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
"B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
"C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
"D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
"E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
"F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
);
foreach ($graph as $key => $value) {
$val = str_replace("[","{",$value);
$val = str_replace("]","}",$val);
$val = str_replace("'",'"',$val);
$val = str_replace("=>",":",$val);
$val = rtrim($val, ',');
$graph[$key] = json_decode($val, true);
}
echo "<pre>";
print_r($graph);
echo "</pre>";
我下载了您的
文件并在本地运行,以便源与您所说的完全一样。然后我继续解析它并将字符串值转换为实际的数组 现在的样子:
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
最终结果:
// Parse graph.json file
$json = json_decode(file_get_contents('graph.json'), true);
foreach ($json as $key => $value) {
foreach ($value as $k => $val) {
$str = str_replace(['[', ']'], '', $val);
$str = str_replace(' => ', ',', $str);
$str = str_replace("'", "", $str);
$str = explode(',', $str);
for ($x = 0; $x < count($str); $x = $x + 2) {
$graph[$k][trim($str[$x])] = $str[$x+1];
}
}
}
// Result
echo "<pre>";
print_r($graph);
// Proof it is an array now (result 3)
// echo '<pre>';
// print_r($graph['A']['B']);
如果您运行下面的预期输出示例,然后将输出与我的输出进行比较,它是相同的:
Array
(
[A] => Array
(
[B] => 3
[C] => 5
[D] => 9
)
[B] => Array
(
[A] => 3
[C] => 3
[D] => 4
[E] => 7
)
[C] => Array
(
[A] => 5
[B] => 3
[D] => 2
[E] => 6
[F] => 3
)
[D] => Array
(
[A] => 9
[B] => 4
[C] => 2
[E] => 2
[F] => 2
)
[E] => Array
(
[B] => 7
[C] => 6
[D] => 2
[F] => 5
)
[F] => Array
(
[C] => 3
[D] => 2
[E] => 5
)
)
$graph = [
'A' => ['B' => 3, 'C' => 5, 'D' => 9],
'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
'F' => ['C' => 3, 'D' => 2, 'E' => 5],
];
echo '<pre>';
print_r($graph);
但请记住:评估是邪恶的。如果您不信任输入,则需要编写自己的解析器。
为了澄清而进行的临时编辑。这是我运行你的 github 示例时得到的结果
//or $graph instead of $result
$result = array_map(function($value) {
//use eval to directly evaluate the string
//we just need to remove the trailing comma
//and add a semicolon
eval('$ret = '.rtrim($value,',').';');
return($ret);
}, $array); // replace $array with the var name of your array!
:
json_decode(...,true)
这与你的问题不同。