如何从数组中删除不需要的值

19 浏览
0 Comments

如何从数组中删除不需要的值

有人知道我为什么会得到这种数组吗?我只想要下面这部分。我需要从中删除那些mysql连接和其他不需要的数组。

array (
  'name' => 'Westwood',
  'id' => 538,
),

 0 => 
  Common\Models\Property::__set_state(array(
     'connection' => 'mysql',
     'table' => NULL,
     'primaryKey' => 'id',
     'keyType' => 'int',
     'incrementing' => true,
     'with' => 
    array (
    ),
     'perPage' => 15,
     'exists' => true,
     'wasRecentlyCreated' => false,
     'attributes' => 
    array (
      'name' => 'Westwood',
      'id' => 538,
    ),
     'guarded' => 
    array (
      0 => '*',
    ),
  )),

下面的代码显示了我获取该数组的方法。当我Log ::info($results);时,我得到了那个数组。希望您能理解我的问题。

$properties = model::where('status', '=', 'Active')
                ->get();
if($jsonData->city !== "") {
    foreach ($properties as $property) {
        if($property->city === $jsonData->city) {
            $results[] = $property;
        }
    }
}

admin 更改状态以发布 2023年5月22日
0
0 Comments

根据get()手册所述,get方法返回一个Illuminate\\Support\\Collection对象,其中每个结果都是PHP stdClass对象的实例。您可以通过将列视为对象的属性来访问每个列的值。\n\n它将返回一个laravel集合。\n\n因此,请使用toArray()来获取Array。\n\nHow to Create Multiple Where Clause Query Using Laravel Eloquent?引用: 同时指定字段名称以缩小您的数组范围。\n\n

$properties = prop_model::select('name','id')
                ->where(['status', '=', 'Active'],['propertylive', '=', 'Yes'])
                ->get()
                ->toArray();

0
0 Comments

由于您正在使用get(),它将返回一个laravel集合,而不是一个数组。如果您想要一个数组,可以使用toArray(),它看起来像这样:

$properties = prop_model::where('status', '=', 'Active')
                ->where('propertylive', '=', 'Yes')
                ->get()
                ->toArray();

0