简体   繁体   English

codeigniter数组json和flashdata

[英]codeigniter array json and flashdata

I am using Codeigniter 3 and getting data from an API. 我正在使用Codeigniter 3,并从API获取数据。 The API returns the below after I pass the origin json data through, $myArray = json_decode($theBody, true); 在我将原始json数据通过$myArray = json_decode($theBody, true);之后,API返回以下内容

array(2) {
  ["status"]=>
  string(7) "failure"
  ["message"]=>
  array(2) {
    ["entry_name"]=>
    string(61) "The entry_name field must be at least 8 characters in length."
    ["entry_body"]=>
    string(61) "The entry_body field must be at least 8 characters in length."
  }
}

I now want to pass that error message via flashdata to my view which I do as follows: 我现在想通过flashdata将该错误消息传递到我的视图,该操作如下:

// VIEW FILENAME: new.php
$this->session->set_flashdata('message', $myArray);

In my view, when I run this: 在我看来,运行此命令时:

       echo "<pre>";
        echo var_dump($this->session->flashdata('message'));
        echo "</pre>";

I get the expected output (same as above): 我得到了预期的输出(与上面相同):

   array(2) {
  ["status"]=>
  string(7) "failure"
  ["message"]=>
  array(2) {
    ["entry_name"]=>
    string(61) "The entry_name field must be at least 8 characters in length."
    ["entry_body"]=>
    string(61) "The entry_body field must be at least 8 characters in length."
  }
}

However, how can I iterate through the array? 但是,如何遍历数组?

How can I refer to the contents of ["status"] and ["message"] 如何参考["status"]["message"]

Any pointers appreciated. 任何指针表示赞赏。

Take the values in the variable and navigate to the array values as follows: 取变量中的值,并导航到数组值,如下所示:

$flashData = $this->session->flashdata('message');
$status = $flashData['status'];
$message = $flashData['message'];
$entry_name = $flashData['message']['entry_name'];
$entry_body = $flashData['message']['entry_body'];

Check the array How its coming through, If Zero indexed add [0] front of the array pointer. 检查数组如何通过,如果索引为零,则在数组指针前面添加[0] (Ex: $flashData[0]['entry_name'] ) (例如: $flashData[0]['entry_name']

you can access flashdata fields by its key since its an associative array. 您可以通过其键访问flashdata字段,因为它是关联数组。

$flashdata = $this->session->flashdata('message');
$status = $flashdata['status'];

As message is an array, iterate through array to fetch its value. 由于message是一个数组,因此遍历数组以获取其值。

foreach($flashdata['message'] as $key => $value){
     echo $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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