[PHP] Use of callback on a stream

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-28-2008
Olivier Dupuis
 
Posts: n/a
Default [PHP] Use of callback on a stream

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


Reply With Quote
  #2 (permalink)  
Old 03-28-2008
Bagus Nugroho
 
Posts: n/a
Default array_filter function

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








Reply With Quote
  #3 (permalink)  
Old 03-28-2008
Richard Heyes
 
Posts: n/a
Default Re: [PHP] array_filter function

> 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
Reply With Quote
  #4 (permalink)  
Old 03-28-2008
Robin Vickery
 
Posts: n/a
Default 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";'));
Reply With Quote
  #5 (permalink)  
Old 03-28-2008
Bagus Nugroho
 
Posts: n/a
Default RE: [PHP] array_filter function

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




Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:56 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0