dpqaaczn141761 2017-12-18 15:13
浏览 55
已采纳

如何拆分字符串并构建一个关联数组php?

How to split a string in PHP ? For example, if i have the string like

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

how can i get

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

and later i want to build an associative array like for example,

$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条回答 默认 最新

  • douxun3496 2017-12-18 15:40
    关注

    From your examples, I'm guessing you want to split the string by commas followed by the number 1. To do this, you can use preg_split() with a positive lookahead:

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

    Gives:

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

    This function will parse out the whole string into a nested array:

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

    Tested as follows (in php 5.6):

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

    Gives this output:

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

    You could then use array_combine() for example to merge in the names:

    $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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题