我不知道如何有效地将此数组的值从字符串转换为数组。我真的很感谢任何建议。
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],
];
答案 0 :(得分:4)
似乎您正在尝试将数组字符串转换为数组。
您可以重复循环或通过make函数获得所需的输出。
我在 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
)
)
实时demo
说明:
$ 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]
是{ {1}}。
preg_match_all如何工作?
$graph[$k]
它匹配字符串中的模式,然后将结果作为数组分配给$k
。
了解更多信息。
preg_match_all
regex pattern cheat sheet
注意::我们使用的是非捕获模式,因此它同时返回完全匹配项和所需的匹配项...因此我们在A
中找到了所需的记录。
答案 1 :(得分:1)
有点丑陋,但我认为这终于可以解决问题了。
我下载了您的 file 并在本地运行,以确保源与您所说的完全相同。然后我继续解析它,并将字符串值转换为实际数组
这是现在的样子:
// 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);
答案 2 :(得分: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
)
)
答案 3 :(得分:-1)
正确的答案是:不要创建一个奇怪的数组;)但是,既然您这样做了,就应该做到这一点:
//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!
但是请记住:评估是邪恶的。如果您不信任输入,则需要编写自己的解析器。
临时编辑以进行澄清。这是我在运行您的github示例槽json_decode(...,true)
时得到的结果:
array(6) {
[0]=>
array(1) {
["A"]=>
string(30) "['B' => 3, 'C' => 5, 'D' => 9]"
}
[1]=>
array(1) {
["B"]=>
string(40) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7]"
}
[2]=>
array(1) {
["C"]=>
string(50) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3]"
}
[3]=>
array(1) {
["D"]=>
string(50) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2]"
}
[4]=>
array(1) {
["E"]=>
string(40) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5]"
}
[5]=>
array(1) {
["F"]=>
string(30) "['C' => 3, 'D' => 2, 'E' => 5]"
}
}
这与您的问题不同。