2017-09-05 3 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つに似たロジックを実行していますが、それは機能していますが、今は私の指を誤植に入れているように見えます。 誰かが私を助けますか?

+1

あなたは入力データを誤解しています。あなたは完全に有効な配列ダンプのような "見た目"の文字列を持っています。データを必要に応じて簡単にアクセスできるように、データを別々に保存する必要があります。このファイルがあなたのコントロールから外れているならば、あなたは正規表現か何かでそれをハックする必要があります。 – mickmackusa

+0

あなたはこれを次のように切り詰めるかもしれません:https://regex101.com/r/3CXktP/1 – mickmackusa

+0

私は答えを投稿しますか? – mickmackusa

答えて

0

$bodyは文字列ではないファイルの内容を返します。file_get_contentsは文字列であるため、配列は文字列ではありません。したがって、配列のように$bodyで作業することはできません。

文字列のデータをregexpで解析できます(と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の内容を制御できませんANDアレイの構造が信頼性がある場合は、あなたがそれを行うことができます最高の正規表現でそれを解析するを試してみてくださいが、これはしてもしなくてもよい信頼性が高い可能性があります努力。 (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]; 
関連する問題