3

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 ? Commented 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? Commented 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); Commented 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.

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

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).
3
$result =exec($instruction); 
$result_array=explode(' ',$result);

or just

$result =explode(' ',exec($instruction));

12 Comments

Thanks Dagon, This worked for me. I found out my $instruction variable also had problems passing the command. Linked here - stackoverflow.com/questions/21545949/…
whats the exact string returned?
they are probably not spaces but something else that looks like it. try var_dump($result); and see what that returns
This is output from var_dump() string(20) "1 1 1 1 1 1 0 1 0 1 "
worth a try preg_split('/\s+/', $result)
|
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.