2016-08-01 12 views
4

に、ネットワーク上のadbデバイスのリストを格納する方法:コマンドを実行するPHPの配列

$output = shell_exec("adb devices -l"); 

echo "$output"; 

と、このような出力を得る:

List of devices attached 
10.16.21.138:5555  device product:tbltevzw model:SM_N915V device:tbltevzw 
10.16.20.90:5555  device product:ghost_verizon model:XT1060 device:ghost 

PHPの連想にこの情報を格納するためのいくつかのいずれかを助けることができますアレイ?

答えて

2
// remove extra spaces, then split into array by new lines 
$arr = explode("\n", preg_replace('/ +/',' ',$x)); 
array_shift($arr); // remove 1-st line 
$result = Array(); 
foreach($arr as $v) 
{ 
    $tmp = explode(' ',$v); // split each line into words 
    list($ip,$port) = explode(':',$tmp[0]); 
    $item = Array('ip'=>$ip, 'port'=>$port);  
    array_shift($tmp); // remove IP:PORT 
    array_shift($tmp); // remove DEVICE 
    foreach($tmp as $n) 
    { 
     list($key,$data) = explode(':',$n); // split by : 
     $item[$key] = $data; // accumulate key/data pairs 
    } 
    $result[] = $item; // append device to the result array 
} 
+0

ありがとうございました。それは有用だった – user2786092