This is a discussion on [PHP] Use of callback on a stream within the PHP General forums, part of the PHP Programming Forums category; Hello, I am trying to make a daemon that launches shell commands via proc_open and gets the stdout of the ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
I am trying to make a daemon that launches shell commands via proc_open and gets the stdout of the command via a pipe. The thing is that I would like to get this stdout via a callback, instead of monitoring the pipe regularly. I tried the following code (this is a simplified version of it, but the callback never gets called, does anyone know what I forgot ? Thanks, Olivier <?php // First we proc_open a ls // 1 - we build the descriptor array $descriptorspec = array( 0 => array("pipe", "r"), // // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w") // stderr ); $pipes=array(); // We make a simple callback function function stream_callback(){ global $pipes; $stdout_child=stream_get_contents($pipes[1]); print("Callback received:\n"); print($stdout_child."\n"); } $context=stream_context_create(array()); stream_context_set_params($context,array('notifica tion' => 'stream_callback')); $handle=proc_open("/bin/ls /tmp",$descriptorspec,$pipes,null,null,array('conte xt' => $context)); stream_context_set_params($pipes[1],array('notification' => 'stream_callback')); while (true){ print("Waiting...\n"); sleep(5); } |
|
|||
|
Hello,
If I have an array like this $dataArray = array(0=>array('type'=>'da'), 1=>array('type'=>'wb'), 2=>array('type'=>'da'); How I can filtering to get only 'da' only, like this $newDataArray = array(0=>array('type'=>'da'),2=>array('type'=>'da' )) Thanks in advance bnug |
|
|||
|
> If I have an array like this
> $dataArray = array(0=>array('type'=>'da'), 1=>array('type'=>'wb'), 2=>array('type'=>'da'); > > How I can filtering to get only 'da' only, like this > > $newDataArray = array(0=>array('type'=>'da'),2=>array('type'=>'da' )) Off the top of my head: <?php foreach ($newDataArray as $k => $v) { if (!empty($v['type']) AND $v['type'] != 'da') { unset($newDataArray[$k]); } } ?> Optionally, you could use array_values() to re-index $newDataArray if you need to. -- Richard Heyes Employ me: http://www.phpguru.org/cv |
|
|||
|
On 28/03/2008, Bagus Nugroho <bnugroho@unisemgroup.com> wrote:
> Hello, > > If I have an array like this > $dataArray = array(0=>array('type'=>'da'), 1=>array('type'=>'wb'), 2=>array('type'=>'da'); > > How I can filtering to get only 'da' only, like this > > $newDataArray = array(0=>array('type'=>'da'),2=>array('type'=>'da' )) $newDataArray = array_filter($dataArray, create_function('$a','return $a["type"] == "da";')); |
|
|||
|
Thanks You,
rgds, bnug ________________________________ From: Robin Vickery [mailto:robinv@gmail.com] Sent: Jumat 28-Mar-2008 21:45 To: Bagus Nugroho Cc: php-general@lists.php.net Subject: Re: [php] array_filter function On 28/03/2008, Bagus Nugroho <bnugroho@unisemgroup.com> wrote: > Hello, > > If I have an array like this > $dataArray = array(0=>array('type'=>'da'), 1=>array('type'=>'wb'), 2=>array('type'=>'da'); > > How I can filtering to get only 'da' only, like this > > $newDataArray = array(0=>array('type'=>'da'),2=>array('type'=>'da' )) $newDataArray = array_filter($dataArray, create_function('$a','return $a["type"] == "da";')); |
![]() |
| Thread Tools | |
| Display Modes | |
|
|