-2

如何使用 php 从以下行获取每个结果?

ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000

我尝试了爆炸和 foreach 但没有成功。谢谢!

4

2 回答 2

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
)
于 2013-09-06T16:49:29.173 回答
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);

在这里,您将从单元格中获取数组键作为变量名和数组值作为其值。

于 2013-09-06T16:45:49.563 回答