Php 如何拆分字符串并构建关联数组?
如何在PHP中拆分字符串?例如,如果我有如下字符串Php 如何拆分字符串并构建关联数组?,php,wordpress,split,Php,Wordpress,Split,如何在PHP中拆分字符串?例如,如果我有如下字符串 Array([0] => "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7"); 我怎样才能得到它 Array([0] => 1=>10,2=>9,3=>7 [1] => 1=>9,2=>8,3=>7); 然后我想构建一个关联数组,比如 $ratings = array(6533 => ['Build Quality' =>
Array([0] => "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7");
我怎样才能得到它
Array([0] => 1=>10,2=>9,3=>7 [1] => 1=>9,2=>8,3=>7);
然后我想构建一个关联数组,比如
$ratings = array(6533 => ['Build Quality' => [1=>10,2=>9,3=>7],
'Versatility' => [1=>9,2=>8,3=>7],
'value' => [1=>9.5,2=>7,3=>6]]);
//takes the current post id and returns an product ratings array
function get_ratings($current_post_id){
$product_post = get_post($current_post_id);
preg_match_all("/\[v360_product_table\s.*?\]/", $product_post>post_content, $product_elements);
$product_elements = $product_elements[0][0];
preg_match_all('/"([^"]+)"/', $product_elements, $parameters);
$product_params = $parameters[0][0];
$rating_params = preg_split('","', $product_params);
$rating_factors = str_replace('"', '', $rating_params);
$b = print_r($rating_factors);
/* output: Array ( [0] => Build Quality [1] => Versatality [2] => Adoptability) */
$product_rank = $parameters[0][1];
/* output: Array ( [0] => 1=>10,2=>9,3=>7,1=>9,2=>8,3=>7 ) */
$rank_split = preg_split('"**have to split it here**"', $product_rank);
$rank_values = str_replace('"', '', $rank_split);
$assoc_array = array_combine($rating_factors, $rank_values);
/* needs to construct an array like '$ratings' */
$ratings = array(6533 => ['Build Quality' => [1 => 10, 2 => 8, 3 => 7],
'Versatility' => [1 => 9, 2 => 9, 3 => 8], 'Value' => [1 => 10, 2 => 8,3 => 8]]);
return $ratings[$current_post_id];
}
从您的示例中,我猜您希望用逗号和数字1拆分字符串。要做到这一点,您可以使用具有正向前瞻性的
preg_split()
:
$string = "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7";
$split = preg_split('/,(?=1\b)/', $string);
var_dump($split);
给出:
array(2) {
[0]=>
string(15) "1=>10,2=>9,3=>7"
[1]=>
string(14) "1=>9,2=>8,3=>7"
}
此函数将把整个字符串解析为嵌套数组:
function split_string($string)
{
$split = array();
foreach (preg_split('/,(?=1\b)/', $string) as $row => $part1) {
foreach (explode(',', $part1) as $part2) {
list($key, $value) = explode('=>', $part2, 2);
$split[$row][$key] = $value;
}
}
return $split;
}
测试如下(在php 5.6中):
给出此输出:
array(2) {
[0]=>
array(3) {
[1]=>
string(2) "10"
[2]=>
string(1) "9"
[3]=>
string(1) "7"
}
[1]=>
array(3) {
[1]=>
string(1) "9"
[2]=>
string(1) "8"
[3]=>
string(1) "7"
}
}
然后,您可以使用array\u combine()
合并名称:
$string = "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7";
$split = split_string($string);
var_dump(array_combine(array('Build Quality', 'Versatility'), $split));
拆分数组的规则是什么?您的问题中还不清楚。您还可以输入您迄今为止尝试过的任何代码。这是从哪里来的
'value'=>[1=>9.5,2=>7,3=>6]
?您还可以告诉我们初始字符串是从哪里来的吗?改变这一点可能更容易…您可以使用explode()
函数在PHP中拆分字符串。我建议首先在,
上拆分以获得每个单独的键/值对,然后在它们上循环并在=>
上拆分每个键以将键与值分开。然后你可以用它们来构建你想要的数组。我尝试过preg_split(regex模式)、str_split(x个字符后)和explode(,delimiter),但无法实现我所提到的格式。可以应用任何规则,但需要使用此格式。
$string = "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7";
$split = split_string($string);
var_dump(array_combine(array('Build Quality', 'Versatility'), $split));