简体   繁体   中英

I am getting error Illegal string offset 'requestId'

i using API , and it return me some data in my url in json :

this is actual url data :

data={%22requestId%22%3A%22546b384ce51f469a2e8b4567%22%2C%22numbers%22%3A{%22917566559950%22%3A{%22date%22%3A%222014-11-18+17%3A45%3A59%22%2C%22status%22%3A1%2C%22desc%22%3A%22DELIVERED%22}}}

my PHP code is for insert data in to database but getting error :

$request = $_REQUEST["data"];
$jsonData = json_decode($request,true);
$link = mysqli_connect("127.0.0.1", "root", "", "table");
 foreach($jsonData as $key => $value)
 {
  $requestID = $value['requestId'] ;
  $userId = $value['userId'];
   $senderId = $value['senderId'];
    foreach($value['report'] as $key1 => $value1)
      {
    //detail description of report
    $desc = $value1['desc'];
    // status of each number
    $status = $value1['status'];
    // destination number
    $receiver = $value1['number'];
    //delivery report time
    $date = $value1['date'];
    $query = "INSERT Query for store record ";
    mysqli_query($link, $query);
}
 }

what is problem here error is Warning: Illegal string offset 'requestId' on line 11 Warning: Illegal string offset 'userId' ..... please solve it....

TLDR;

You should remove the foreach($jsonData as ...) around your code.

You are foreaching on this associative array with it's keys:

{  
"requestId":"546b384ce51f469a2e8b4567",
"numbers":{  
   "917566559950":{  
     "date":"2014-11-18 17:45:59",
     "status":1,
     "desc":"DELIVERED"
  }
 }
}

The first iteration the key will be "requestId" and the value field will be "546b384ce51f469a2e8b4567". At the second round you got "numbers" and the array above.

$requestID = $jsonData['requestId'];
var_dump($requestID); // the requestID


// foreaching in the assoc array inside the 'numbers'
foreach ($jsonData["numbers"] as $key => $value) {
  // description
  $desc = $value['desc'];
  // status of each number
  $status = $value['status'];
  // date
  $date = $value['date'];    

  var_dump($key); // the key with 917...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM