我无法将Json转换为字符串[OctoberCms]

时间:2016-06-01 16:32:23

标签: php json laravel octobercms

我正在尝试从输入中获取值,该输入允许用户创建他想要的多个字段,这就是我在fields.yaml中创建该字段的方式。

str_og_fb_admins:
        label: Facebook admins
        comment: Insert here the admins names
        span: left
        tab: Facebook
        type: repeater
        form:
          fields:
            str_og_fb_admins:
              label: Facebook admins
              type: text

然后在我的组件上我这样称呼它:

$settings = Settings::instance();
$this->ogFbAdmins = $settings->str_og_fb_admins;

但现在我想从每个单数输入中获取单个值,我不知道如何,我尝试使用json_decode();函数,但它返回错误。

如果我使用echo json_encode($this->ogFbAdmins);,则返回

{"1":{"str_og_fb_admins":"admin1"},"2":{"str_og_fb_admins":"admin2"}}

但是我想让它像这样返回:

admin1
admin2

如何让它以第二种方式返回?

ps:使用json_decode($this->ogFbadmins);时返回的错误是这样的:

“json_decode()期望参数1为字符串,数组为”

2 个答案:

答案 0 :(得分:3)

我认为Start: Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] Hash-Values: [ 6] [ 1] [__] [ 6] [ 0] [ 5] [ 3] [ 0] Number: [ 9] [ 6] [__] [ 2] [ 3] [ 4] [ 5] [ 8] Removing s6 Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] Hash-Values: [ 6] [ 6] [__] [ 6] [ 0] [ 5] [ 3] [ 0] Number: [ 9] [ 2] [__] [ 2] [ 3] [ 4] [ 5] [ 8] Removing s5 Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] Hash-Values: [ 6] [ 6] [__] [ 5] [ 0] [ 5] [__] [ 0] Number: [ 9] [ 2] [__] [ 4] [ 3] [ 4] [__] [ 8] 是一个数组,所以你根本不需要任何JSON转换。

尝试将其视为纯PHP中的数组

$this->ogFbAdmins

现在内部结构可能是一个对象,所以你可能需要做

$settings = Settings::instance();
$this->ogFbAdmins = $settings->str_og_fb_admins;
foreach ($this->ogFbAdmins as $adm) {
    echo $adm['str_og_fb_admins'];
}

答案 1 :(得分:2)

这应该有效:

<强> PHP

$ogFbAdmins = json_decode(json_encode($this->ogFbAdmins), true);
foreach($ogFbAdmins as $key => $value) {
  echo $value['str_og_fb_admins'] . "\n";
}  

EVAL.IN