array(   [0] => 'ttt',   [1] => '111'"/> array(   [0] => 'ttt',   [1] => '111'">

首頁 >後端開發 >php教程 > 字符串处置成数组

字符串处置成数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原創
2016-06-13 13:07:25856瀏覽

字符串处理成数组
请教大牛们,如何将该字符串"{SUCESS=[ttt,111],ERROR=[rrr,ggg,vd]}"处理成数组
如下
array(
  'SUCESS' => array(
  [0] => 'ttt',
  [1] => '111'
  ),
  'ERROR'=> array(
  [0] => 'rrr',
  [1] => 'ggg',
  [2] => 'vd'
  )


------解决方案--------------------

PHP code
$s="{SUCESS=[ttt,111],ERROR=[rrr,ggg,vd]}";
preg_replace('/(\w+)=\[(.*?)\]/e','$ar[$1]=explode(",","$2")',$s);
print_r($ar);
/*
Array
(
    [SUCESS] => Array
        (
            [0] => ttt
            [1] => 111
        )

    [ERROR] => Array
        (
            [0] => rrr
            [1] => ggg
            [2] => vd
        )

)
*/ <div class="clear">
                 
              
              
        
            </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn