2

I have a Linux web server running a PHP/HTML page.

I need to save an output that has to interpreted as an array -

    exec($instruction);

where the output will be

    1 2 5 7 0 5 3 4

and I must be able to call out particular element in the array

    echo $result[4]

so far the following attempts were unsuccessful

    $result =exec($instruction); 
    or
    $result = array(exec($instruction));

Update, So far I tried this -

$result = shell_exec($instruction);
$out = explode(" ",$result);

I got the expected output, but why doesn't exxplode() return individual elements?

Array ( [0] => 1 1 1 2 1 2 0 0 1 1 )
3
  • 1
    explode on the space ?
    – user557846
    Feb 4, 2014 at 3:34
  • Thanks, That solves the part where I have to separate the output, but I'm still not able to get exec() command to output an array. May be there is a better instruction?
    – Tom Iv
    Feb 4, 2014 at 3:43
  • 1
    exec will return a string, which you then convert in to an array via explode. $result =exec($instruction); $result_array=explode(' ',$result);
    – user557846
    Feb 4, 2014 at 3:44

4 Answers 4

14

According to documentation (php.net) exec has a second parameter that is passed by reference called $output. So you can try:

exec($instruction, $results);

And then you can access $results that will be an array with each line as an element. So:

$results[0]

will have the first line of your output.

0
4

Why explode did not work for me? The shell $instruction that I used returned "newline" or "\n". I had to split the string using "\n" as the delimiter. This worked for me -

$result = shell_exec($instruction);
$out = explode("\n",$result);
1
  • 2
    This mode will only save the last iteration. ie, if you have more than 1 named process, like iexplorer windows you will get only last. Consider to use $result[] = shell_exec($instruction); $out = explode("\n",$result[0]); to archieve the same result, but you can be sure about how many proceses you close by count($result).
    – m3nda
    Jul 9, 2014 at 3:59
3
$result =exec($instruction); 
$result_array=explode(' ',$result);

or just

$result =explode(' ',exec($instruction));
12
  • Thanks Dagon, This worked for me. I found out my $instruction variable also had problems passing the command. Linked here - stackoverflow.com/questions/21545949/…
    – Tom Iv
    Feb 4, 2014 at 7:01
  • whats the exact string returned?
    – user557846
    Feb 5, 2014 at 2:57
  • 1
    they are probably not spaces but something else that looks like it. try var_dump($result); and see what that returns
    – user557846
    Feb 5, 2014 at 3:02
  • This is output from var_dump() string(20) "1 1 1 1 1 1 0 1 0 1 "
    – Tom Iv
    Feb 5, 2014 at 3:06
  • worth a try preg_split('/\s+/', $result)
    – user557846
    Feb 5, 2014 at 3:06
0

I prefer to use:

$result = shell_exec($instruction);
$out = explode("\n",$result);

Because shell_exec function returns the complete output as string. If you have more than one line, you should use that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.