-1
PHP에서 문자열을 분할하는 방법은 무엇입니까? 내가어떻게 문자열을 분할하고 연관 배열 PHP를 빌드?
Array([0] => "1=>10,2=>9,3=>7,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];
}
당신이 배열을 분할하는 아래의 규칙은 무엇 - 당신의 문제가 분명하지 않다. 또한 지금까지 시도한 코드를 넣을 수 있습니다. –
이것은 'value'=> [1 => 9.5,2 => 7,3 => 6]'어디에서 왔습니까? – Eddie
초기 문자열의 출처를 알려주시겠습니까? 그것을 바꾸는 것이 더 쉬울 수도 있습니다 ... – Jeff