如何使用php从以下行获取每个结果?
我尝试了爆炸和foreach但没有成功。谢谢!
答案 0 :(得分:0)
请尝试以下操作:
$str = "ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000";
$final_array = array();
$data_array = explode(';', $str);
foreach($data_array as $single_data)
{
$single_data = trim($single_data);
$single_unit = explode('=', $single_data);
$single_unit[0] = trim($single_unit[0]);
$single_unit[1] = trim($single_unit[1]);
$final_array[$single_unit[0]] = $single_unit[1];
}
print_r($final_array);
在这里,您将获取数组键作为变量名称和数组值作为其来自单元格的值。
答案 1 :(得分:0)
$text = "ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000";
$exploded = explode(';', $text);
foreach($exploded as $data)
{
$temp = explode('=', $data);
$result .= 'Value of "' . $temp[0] . '" is: ' . $temp[1] . '<br>';
}
echo $result;
<强>输出强>:
Value of "ssrc" is: 15012312307
Value of "themssrc" is: 2790404163
Value of "lp" is: 0
Value of "rxjitter" is: 0.001079
Value of "rxcount" is: 933
Value of "txjitter" is: 0.000000
Value of "txcount" is: 735
Value of "rlp" is: 0
Value of "rtt" is: 0.002000
您可以在foreach中编辑此代码以满足您的要求。例如。数组:
$result = Array();
foreach($exploded as $data)
{
$temp = explode('=', $data);
$result[$temp[0]] = $temp[1];
}
print_r($result);
<强>输出强>:
Array
(
[ssrc] => 15012312307
[themssrc] => 2790404163
[lp] => 0
[rxjitter] => 0.001079
[rxcount] => 933
[txjitter] => 0.000000
[txcount] => 735
[rlp] => 0
[rtt] => 0.002000
)