【问题标题】:Get data from object php laravel从对象 php laravel 获取数据
【发布时间】:2021-02-04 07:32:03
【问题描述】:

大家好,我需要一些帮助,

我的变量$a 的值[{"id":"[1, 2, 3]"}] 类型为object,现在我想要第一个id,即在这种情况下我想要1。如何从变量 $a 中获取 ut?

当我 var_dump 打印如下

class Illuminate\Support\Collection#1230 (1) {
  protected $items =>
  array(1) {
    [0] =>
    class stdClass#1221 (1) {
      public $a =>
      string(9) "[1, 2, 3]"
    }
  }
}

谢谢。

【问题讨论】:

  • 试试($a[0])->id[1]

标签: php arrays laravel object


【解决方案1】:

$aclass 是什么?
它是Illuminate\Database\Eloquent\Model 的扩展吗?

也许是这样的:

$firstId = current($a->id);

编辑:

我在您的转储中没有看到 id

current(json_decode($a->first()->id))

current(json_decode($a->first()->a))

【讨论】:

  • 我已经更新了这个问题你能检查一次吗
  • 非常感谢。你拯救了我的一天!
【解决方案2】:

你可以访问吗?

echo $a->id; // outputs strings "[1,2,3]"

如果是,则只需使用 json_decode 对其进行解码

$array = json_decode($a->id);
$firstElement = current($array);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2013-08-11
    • 1970-01-01
    • 2019-06-05
    • 2017-07-08
    • 1970-01-01
    相关资源
    最近更新 更多