Link to home
Start Free TrialLog in
Avatar of aej1973
aej1973

asked on

print array using php cli..

Hi, I have a script that give me the following output on my screen ;

Number               Place            status               time
100                        A                 ACTIVE             10ms
200                        B                 ACTIVE             15ms
300                        C                 INACTIVE          

I need to get this output in an array form as follows using foreach and the explode function, how can this be done?


100’ => array(
‘Place’=>‘A’,
‘Status’=>’Active’,
‘Time’ => ‘10’),

‘200’ => array(
‘Place’=>‘B’,
‘status’=>’Active’,
‘Time’=>’15’),

‘300’ => array(
‘Place’=>‘C’,
‘Status’=>’Inactive’,
‘Time’=>’ ’),  

);

Thank you for the help
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

That's somewhat confusing since you already did it.  If you want to 'automate' something, you need to tell us how you are getting the data and what format it is in when you get it.
Avatar of aej1973
aej1973

ASKER

Hi Dave, sorry for not making my self clear. I have a class file which I use to run a command as follows:

 $peer = $asm->command("sip show peers");

The 'sip show peers'  will give an output in the format that I had mentioned above. I would like to manipulate the output (ie) $peer to be able to pick out certain sections of the output. What I tried was as follows:

$data = array();
     foreach(explode(" ", $peer['data']) as $line)          
          print_r($data);


but I guess I am doing something wrong as that does not work. This is where I need some help. Thank you.

A
since you have : $peer['data'] as $line , isn't

print_r($line) the result you are looking for ?  If you were to do print_r($data), the foreach line does not seem to make sense.

Thanks
Amit
$data = array();
foreach(explode(" ", $peer['data']) as $line)          
   print_r($data);


When you write $data = array(); you create an empty array or, if the array already exists, you empty it. This means that in this piece of code you're trying to print an empty array each loop on another array.

Secondly, the line foreach(explode(" ", $peer['data']) as $line)   can't never work: explode function returns an array buit with several string parts obtained splitting an original string using a symbol, in your case a space, so what element of this array are you trying to use in the loop?

Please, post here what is $peer['data'] and how is tructured (write

echo "<pre>";
var_dump($peer['data']);
echo "</pre>";

and copy/paste here what is printed on the screen)

Cheers
If $peer is giving you values in fixed positions as you have shown, you can use explode http://us2.php.net/manual/en/function.explode.php to split it into lines ( explode("\\n", $peer);  and substr http://us2.php.net/manual/en/function.substr.php to extract the values.
Avatar of aej1973

ASKER

The output of my command  foreach(explode("\n\r ",$peer['data']) as $obj_key => $value); is attached as an image. Now I need to know how to extract the individual fields using substr ( or any other function ).Can you let me know how this can be done, thanks for the help.

A
output.png
Is each row a single string? Is that output an array of string? What is $obj_keya and what the $value? Without these infos I'm not able to help you.:)
Avatar of aej1973

ASKER

Hi marquesG,  the output of the following command :

echo "<pre>";
var_dump($peer['data']);
echo "</pre>";


gave an output exactly as the screen shot I had attached. I am not sure if that answers your question, thank you for your help.

A
I'll do some test and I'll come back to you later...
Avatar of aej1973

ASKER

Thank you.
Well, try to see if this makes sense for you:
$data = array(
    array(
        "Number"=>"100",
        "Place"=>"A",
        "status"=>"ACTIVE",
        "time"=>"10ms"
        ),
    array(
        "Number"=>"200",
        "Place"=>"B",
        "status"=>"ACTIVE",
        "time"=>"15ms"
        ),
    array(
        "Number"=>"300",
        "Place"=>"C",
        "status"=>"INACTIVE",
        "time"=>"20ms"
        ),
    );    
echo "<pre>";
var_dump($data);
echo "</pre>";

$newdata = array();
foreach ($data as $d){
  $newdata[$d['Number']] = array("Place"=>$d['Place'], "status"=>$d['status'], "time"=>$d['time']);
}
echo "<pre>";
var_dump($newdata);
echo "</pre>";

Open in new window


The $data array should be your output and $newdata should be the array as you would want it

Cheers
Avatar of aej1973

ASKER

Thanks Marcus, the only issue here is that, I would like the output ( as shown in the image) to be converted into an array form first. The reason being, I will have several rows, but yes you are absolutely correct about the $data array and $newdata array. Thank you.
How is this whole code?

 $peer = $asm->command("sip show peers");

Can you post it here? Probably I'm missing something essential, but I can't play with data without data and that image give me nothing. I see some words and number formatted in something like a table: is it a html table? Is it something else? How that data are produced?
You should post here the code of the class method you are using...
Avatar of aej1973

ASKER

Well Marcus,

I am not sure how to explain this but even if I post the complete code it may be difficult to understand as this whole project is about building an asterisk PBX system. The 'sip show peers'  runs a query that shows all the extensions that are available and some details regarding these phone extensions ( each line in the image file shows the details of a configured phone extension). I run this command through a php cli code that give me the out put on my command line interface. What I am trying to do is now manipulate that output to be able to pick up certain fields which I will need to pass to a webpage and the first step in doing this is find a way to get the data. Dave had mention using the explode function and the substr function to do this and I think that would be the right direction but I am not sure how to piece this all together. Thank you very much for you help.


A
Now I have to go away, but maybe I have a solution in mymind ;-) I'll came back later

Cheers
Avatar of aej1973

ASKER

Thanks Marcus, I appreciate your help.

Regards,
A
Avatar of aej1973

ASKER

Attached the screen shot of var_dump, thanks.
output.png
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aej1973

ASKER

Thank you Marcus this is what I was looking from, had to make a few changes. Thank you very much.

A
I'm happy for having helped you. Good luck with your project and thanks for points

Cheers
Marco