2017-12-18 1 views
-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]; 
     } 
+2

당신이 배열을 분할하는 아래의 규칙은 무엇 - 당신의 문제가 분명하지 않다. 또한 지금까지 시도한 코드를 넣을 수 있습니다. –

+0

이것은 'value'=> [1 => 9.5,2 => 7,3 => 6]'어디에서 왔습니까? – Eddie

+0

초기 문자열의 출처를 알려주시겠습니까? 그것을 바꾸는 것이 더 쉬울 수도 있습니다 ... – Jeff

답변

0

을 내가 어떻게

Array([0] => 1=>10,2=>9,3=>7 [1] => 1=>9,2=>8,3=>7); 

를 얻을 수 있습니다 나중에 내가 예를 들어 같은 연관 배열을 구축하려는 , 문자열을 쉼표로 나눠서 숫자 1 뒤에 나눠 넣으려는 것 같습니다. 이렇게하려면 preg_split()을 긍정적 인 선견자로 사용하십시오 :

,363,210
$string = "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7"; 
$split = preg_split('/,(?=1\b)/', $string); 
var_dump($split); 

준다 (PHP 5.6) 다음

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; 
} 

테스트 :

array(2) { 
    [0]=> 
    string(15) "1=>10,2=>9,3=>7" 
    [1]=> 
    string(14) "1=>9,2=>8,3=>7" 
} 

이 함수는 중첩 배열에 전체 문자열을 파싱

$string = "1=>10,2=>9,3=>7,1=>9,2=>8,3=>7"; 
$split = split_string($string); 
var_dump($split); 

다음 출력을 제공합니다 :

,
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_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));