我从Asterisk获取包含两列的行,并希望分别处理每一行和每列。
$showmembers = shell_exec('asterisk -rx "database show QM/Exten" |head -n -1 | sed "s#/QM/Exten/##" | tr -d ":" |while read a b; do echo "$a, $b"; done');
这会产生类似:
1111, 2222
3333, 4444
5555, 6666
让我们拨打第一列"号码"和第二个"代理"。 我想在每一行上运行一个foreach循环并回显" number"和"代理"每次迭代都会单独评估。
我最接近的是下面这个:
$showmembers = shell_exec('asterisk -rx "database show QM/Exten" |head -n -1 | sed "s#/QM/Exten/##" | tr -d ":" |while read a b; do echo "$a, $b"; done');
$members = explode(PHP_EOL, $showmembers);
foreach($members as $m) {
echo "$m", PHP_EOL ;
}
它只是输出:
1996, 3079
1997, 3001
1998, 3123
任何提示?
答案 0 :(得分:1)
运行填充代码后,您将拥有2个数组:数组$numbers
,第一列包含数字,数组$agents
包含第二列的代理编号:
$showmembers = shell_exec('asterisk -rx "database show QM/Exten" |head -n -1 | sed "s#/QM/Exten/##" | tr -d ":" |while read a b; do echo "$a, $b"; done');
$members = explode(PHP_EOL, $showmembers);
$numbers = $agents = array();
foreach($members as $m) {
$tmp = explode(",", preg_replace('/ /', '', $m));
$numbers = $tmp[0];
$agents = $tmp[1];
}
答案 1 :(得分:0)
谢谢你们。虽然我不能让你的两个例子都按原样运行,但它确实让我更接近一些有用的东西
我最终使用了以下代码:
$agents = shell_exec("asterisk -rx 'queue show' |grep Agent |awk '{print $1\" \"$2}' |tr -d '(' |sort |uniq");
foreach(explode(PHP_EOL, $agents) as $line)
{
if (preg_match("/Agent/", $line)) {
$agent = preg_split('/ +/', $line);
echo "$agent[0] | ";
echo "$agent[1] <br> ";
}
}