2017-09-05 131 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];