【问题标题】:PHP looping over data and getting key/value pairsPHP循环数据并获取键/值对
【发布时间】:2021-01-15 22:55:47
【问题描述】:

我正在使用 Laravel 8 来循环一些数据。我已经从我的数据库中取出了我的数据,但是我正在努力在foreach 循环中循环数据,它似乎没有给我键/值对,只有索引/值对?

$applicationOptions = ApplicationOptions::where('form_type', $form_type)->get();

foreach ($applicationOptions as $key => $value) {

  // only gives me: 0 - {"id": "3", "name": "john", "age": "20"} ...
  echo "${key} - ${value}";
}

但是如果我var_dump()我的$applicationOptions我可以看到数组里面的属性和原来的??

object(Illuminate\Database\Eloquent\Collection)#279 (1) {
  ["items":protected]=>
  array(1) {
    [0]=>
    object(Company\MyPackage\ApplicationOptions)#289 (27) {
      ["table":protected]=>
      string(38) "inbound_management_application_options"
      ["connection":protected]=>
      string(5) "mysql"
      ["primaryKey":protected]=>
      string(2) "id"
      ["keyType":protected]=>
      string(3) "int"
      ["incrementing"]=>
      bool(true)
      ["with":protected]=>
      array(0) {
      }
      ["withCount":protected]=>
      array(0) {
      }
      ["perPage":protected]=>
      int(15)
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      ["attributes":protected]=>
      array(22) {
        ["id"]=>
        int(3)
        ["name"]=>
        string(6) "john"
        ["age"]=>
        int(20)
      }
      ["original":protected]=>
      array(22) {
        ["id"]=>
        int(3)
        ["name"]=>
        string(6) "john"
        ["age"]=>
        int(20)
      }
      ["changes":protected]=>
      array(0) {
      }
      ["casts":protected]=>
      array(0) {
      }
      ["classCastCache":protected]=>
      array(0) {
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["appends":protected]=>
      array(0) {
      }
      ["dispatchesEvents":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["relations":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["timestamps"]=>
      bool(true)
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
    }
  }
}

我如何访问它?看来我的查询也将整行键/值作为单个对象字符串返回...

【问题讨论】:

  • 它将输出作为集合提供,我不知道你为什么需要 foreach 循环
  • 所以我可以遍历列名并获取相应的值,这似乎可以做到 -> json_decode(json_encode(ApplicationOptions::where('form_type', $form_type)->get()), true)[0];
  • 您可以使用toJson(),将集合转换为json对象。 $applicationOptions = ApplicationOptions::where('form_type', $form_type)->get()->toJson();
  • 你能 var_dump $key$value 吗?

标签: php sql laravel php-7


【解决方案1】:

这是因为您试图循环遍历一个集合,它有多个级别,而包装器只是对象类型。

改用each 方法以获得更简洁的方法。

ApplicationOptions::where('form_type', $form_type)->get()->each(function ($option) {
    echo $option->name:
};

【讨论】:

    【解决方案2】:

    您将获得从数据库返回的每个项目的集合。 这些项目中的每一个都是 ApplicationOptions 的一个实例 键是集合中的索引。并且与数据库无关。

    在 laravel 中,您可以在 Collection 上调用多种方法。 您的数据库将返回 Illuminate/Database/Eloquent/Collection 的实例 您可以阅读收集方法here

    如果您希望检索集合中的每个项目的单个属性值,您可以对结果使用 pluck 方法

    $values = $applicationOptions->pluck('attributeName');
    

    或者,您可以使用 each 方法遍历您的集合

    $applicationOptions->each(function (ApplicationOptions $applicationOption) {
        // insert any logic you want to do.
        echo $applicationOption->attributeName;
    })
    

    如果你想使用结果,map会是更好的选择,你也可以使用mapWithKeys

    $values = $applicationOptions->map(function (ApplicationOptions $applicationOption) {
        // You could also loop through the ApplicationOptions's attributes here using a foreach incase you wish to do something with it.
        return $applicationOption->attributeName;
    })
    
    $valuesWithKeys = $applicationOptions->mapWithKeys(function (ApplicationOptions $applicationOption) {
        return [$applicationOption->attributeNameYouWantToUseForKey => $applicationOption->attributeNameForValue];
    })
    

    如果您只想按特定值作为键,您可以在 sql 查询中执行此操作,或在集合上使用 keyBy 方法

    $values = $applicationOptions->keyBy('attributeName');
    

    最后,请注意,当您尝试将 Model 实例转换为字符串时,laravel 会调用模型上的 toJson 方法。因此,为什么您会在回声中获得 json_encoded 实例。

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 2016-04-29
      • 2014-07-12
      • 2014-08-05
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      相关资源
      最近更新 更多