Stephen
Stephen

Reputation: 149

Multiple rows and arrays

I'm fetching rows with two columns from Asterisk and want to work with every row and column separately.

$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');

This produces something like:

1111, 2222
3333, 4444
5555, 6666

Let's call the first column "number" and the second "agent". I want to run a foreach loop on every line and echo "number" and "agent" values separately in every iteration.

The closest I've gotten was with this below:

$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 ;
}

And it just outputs this:

1996, 3079
1997, 3001
1998, 3123

Any tips?

Upvotes: 1

Views: 87

Answers (2)

Stephen
Stephen

Reputation: 149

Thanks guys. Although I could not get both your examples working as is, it did bring me closer to something that works.
I've ended up using the following code:

$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> ";
}
}

Upvotes: 0

Miro
Miro

Reputation: 1899

After running fillowing code, you will have 2 arrays: array $numbers with numbers from first column and array $agents with agent numbers from second column:

$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];
}

Upvotes: 1

Related Questions