2017-09-05 135 views
-1

我有以下陣列:如何從數組中返回值?

Array 
(
    [0] => 32 
    [1] => Array 
     (
      [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131 
      [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3 
      [status] => failed 
      [direction] => outbound 
     ) 

) 

,並希望得到的ID和狀態,我有以下代碼:

<?php 

require __DIR__ . '/../env.php'; 
//TransferLog Tropo 

ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 


header('Content-Type: application/json'); 
$body = file_get_contents('callnexmo.log'); 
//$body = json_decode($json, true); 

//$id=$_GET['id']; 
//$body=array($id, $json); 

//$req_dump = print_r($body, true); 
//$fp = file_put_contents('callnexmo.log', $req_dump); 

$conn = new mysqli($servername, $username, $password, $database); 

//foreach ($body as $value) 
//{ 
$status=$body[1]['status']; 
$to=$body[1]['to']; 
$from=$body[1]['from']; 
$id=$body[0]; 

echo " body = $body"; 
var_dump($body); 

echo " status = $status"; 
var_dump($status); 


echo " id = $id"; 
var_dump($id); 
//rest of the code 

但產量得到我

Warning: Illegal string offset 'status' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 23 

Warning: Illegal string offset 'to' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 24 

Warning: Illegal string offset 'from' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 25 
body = Array 
(
    [0] => 32 
    [1] => Array 
     (
      [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131 
      [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3 
      [status] => failed 
      [direction] => outbound 
     ) 

) 
string(264) "Array 
(
    [0] => 32 
    [1] => Array 
     (
      [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131 
      [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3 
      [status] => failed 
      [direction] => outbound 
     ) 

) 
" 
status = rstring(1) "r" 
id = Astring(1) "A" 

我一直在做邏輯類似於這個,它有工作,但現在我似乎把我的手指在錯誤.. 有人可以幫助我嗎?

+1

您誤解了傳入的數據。你有一串文字「看起來」像一個完全有效的陣列轉儲。您需要以不同方式存儲數據,以便在需要時隨時訪問它們。如果這個文件超出了你的控制範圍,你將不得不用正則表達式或其他東西來破解它。 – mickmackusa

+0

你可能會這樣砍:https://regex101.com/r/3CXktP/1 – mickmackusa

+0

你想我發表一個答案嗎? – mickmackusa

回答

0

$body是字符串不是數組,因爲file_get_contents返回字符串的文件內容。所以你不能像數組一樣使用$body

您可以使用正則表達式解析字符串中的數據(查看和preg_match_all函數)。

0

實際上,你的第一個斷言是不正確的。你有一個「看起來」像一個數組的字符串。

var_dump()在輸出開始時告訴你:string(264)

根據您的header()聲明,看起來您要處理json字符串。

如果你能控制callnexmo.log的內容,我建議你將你的php數組數據存儲爲json字符串。因此,它看起來像這樣:

[32,{"uuid":"92bbf05c-b49e-4950-9d4a-69c226325131","conversation_uuid":"CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3","status":"failed","direction":"outbound"}] 

然後,當你要處理的.log文件中的數據,你可以json_decode($string,true)字符串生成易於接近的陣列。

如果你沒有過的callnexmo.log內容和陣列的結構是可靠的,那麼你就可以做到這一點嘗試用正則表達式來解析它是最好的,但是這可能是也可能不是一個可靠的控制努力。

鑑於你發佈的數據,這裏有一個簡單的例子:(Pattern Demo)(PHP Demo

$body='Array 
(
    [0] => 32 
    [1] => Array 
     (
      [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131 
      [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3 
      [status] => failed 
      [direction] => outbound 
     ) 

) 
'; 
preg_match_all('/\[([^]]+)\] => (?:Array(*SKIP)(*FAIL)|(.*?)\s+$)/m',$body,$out); 
var_export(array_combine($out[1],$out[2])); 

輸出:

array (
    0 => '32', 
    'uuid' => '92bbf05c-b49e-4950-9d4a-69c226325131', 
    'conversation_uuid' => 'CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3', 
    'status' => 'failed', 
    'direction' => 'outbound', 
) 

這不是你的預期陣列的完美複製品 - 它是扁平化 - 但它應該允許您獲取所需的值。

0

我創建了一個數組,並從那裏獲取了值。

<?php 

require __DIR__ . '/../env.php'; 
//TransferLog Nexmo 

header('Content-Type: application/json'); 
$json = file_get_contents('php://input'); 
$request = json_decode($json, true); 

$id=$_GET['id']; 
$body=array($id, $request); 

$req_dump = print_r($body, true); 
$fp = file_put_contents('callnexmo.log', $req_dump); 

$conn = new mysqli($servername, $username, $password, $database); 

foreach ($body as $value) 
{ 
$status=$body[1]["status"]; 
$to=$body[1]["to"]; 
$from=$body[1]["from"]; 
$id=$body[0];