2016-08-01 3 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