melkawakibi
melkawakibi

Reputation: 881

Get an array from an object | php

How can I get the array from a object? I'm trying to get to the empty array so that I can validate on it's empty state.

$object = Illuminate\Database\Eloquent\Collection Object;

print_r($object);

if(empty($object->array)){

}

output

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
        )

)

Upvotes: 0

Views: 88

Answers (2)

Anand Pandey
Anand Pandey

Reputation: 2025

Try like this:

if ($object->array === array()) {
    echo 'this is explicitly an empty array!';
}

Upvotes: 0

melkawakibi
melkawakibi

Reputation: 881

The object is of the type Eloquent\Collection which means that it has access to a set of methods.

Eloquent\Collection | available methods

Collection provides a set of methods to validate the state of an array inside the Object. Two of which are interesting in the case described by me (OP):

  • IsEmpty
  • IsNotEmpty

Implementation

$website = $this->websitedb->findOneByUrl($this->url);

if($website->isNotEmpty()){

    $uniqueId = rand() . $website[0]->id;

    //save scan to database
    $this->scan = $this->scandb->create($website[0]->id, $uniqueId);

    $this->scandb->createModule($this->scan->id, $options);

}

Upvotes: 2

Related Questions