【问题标题】:How to return values from array?如何从数组中返回值?
【发布时间】:2017-09-05 09:18:32
【问题描述】:

我有以下数组:

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"

我一直在做与这个类似的逻辑,并且它有效,但现在我似乎可以将手指放在错误中.. 有人可以帮我吗?

【问题讨论】:

  • 您误解了传入的数据。你有一个“看起来”像一个完全有效的数组转储的文本字符串。您需要以不同的方式存储数据,以便在需要时可以轻松访问。如果此文件超出您的控制范围,您将不得不使用正则表达式或其他方式破解它。
  • 你可以这样砍它:regex101.com/r/3CXktP/1
  • 您要我发布答案吗?
  • 文件将通过 webhook,我希望稍后将它们保存在数据库中..

标签: php arrays


【解决方案1】:

$body 是字符串而不是数组,因为file_get_contents 返回的文件内容是字符串。所以你不能像使用数组一样使用$body

您可以使用正则表达式解析字符串中的数据(查看pre_matchpreg_match_all 函数)。

【讨论】:

    【解决方案2】:

    实际上,您的第一个断言是不正确的。你有一个“看起来”像数组的字符串。

    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',
    )
    

    它不是您预期数组的完美复制品——它是扁平化的——但它应该允许您获取所需的值。

    【讨论】:

      【解决方案3】:

      我创建了一个数组,并从那里获取了值。

      <?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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        相关资源
        最近更新 更多