2014-10-18 88 views
-2

我有一個這樣的字符串:如何解析字符串數組

"birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s=" 

我想將它解析到PHP數組。怎麼做 ?。非常感謝您的幫助。

+0

使用parse_str()函數 – 2014-10-18 05:05:39

回答

0

對此使用parse_str。 (http://php.net/manual/en/function.parse-str.php

$output = array(); 
parse_str($str, $output); 
echo $output['first']; // value 
echo $output['arr'][0]; // foo bar 
echo $output['arr'][1]; // baz 

或者你可以分析它到本地變量

$str = "first=value&arr[]=foo+bar&arr[]=baz"; 
parse_str($str); 
echo $first; // value 
echo $arr[0]; // foo bar 
echo $arr[1]; // baz 
0

你可以嘗試以下方法: 本的foreach裏面,你會得到所有的GET參數,然後你可以將它們存儲在一個陣列。

foreach($_GET as $key => $value) { 

//your code here 

} 
0
$your_str = 'birs_appointment_price=&birs_appointment_duration=&birs_appointment_alternative_staff=&birs_shortcode_page_url=http%3A%2F%2Flocalhost%2Fstreamline-new%2Fworkshop%2F&_wpnonce=855cbbdefa&_wp_http_referer=%2Fstreamline-new%2Fworkshop%2F&birs_appointment_location=17858&birs_appointment_staff=-1&birs_appointment_avaliable_staff=-1%2C17859&birs_appointment_service=-1&birs_appointment_date=&birs_appointment_notes=&birs_appointment_fields%5B%5D=_birs_appointment_notes&birs_client_type=new&birs_client_name_first=&birs_client_fields%5B%5D=_birs_client_name_first&birs_client_name_last=&birs_client_fields%5B%5D=_birs_client_name_last&birs_client_email=&birs_client_fields%5B%5D=_birs_client_email&birs_field_1=&birs_client_fields%5B%5D=_birs_field_1&birs_client_fields%5B%5D=_birs_field_6&s='; 

$_VARS = array(); 
parse_str($your_str, $_VARS); 

echo $_VARS['birs_appointment_price'];