简体   繁体   中英

Get Values on array from array Laravel PHP

i am making a schedule tasking in laravel, and i need to know the value for other table and, in this case i get the properties and later i save the periods in array from a foreign key in the tables properties, later i need value from "date" in the table periods, but when i get the log i get this.

[2017-07-21 18:11:50] local.INFO: [{"id":5,"title":"Hola","date":"2017-07-25"}] 
[2017-07-21 18:11:50] local.INFO: [{"id":4,"title":"dsfsd","date":"2017-07-26"}]
[2017-07-21 18:11:50] local.INFO: [{"id":4,"title":"dsfsd","date":"2017-07-26"}]
[2017-07-21 18:11:50] local.INFO: [{"id":4,"title":"dsfsd","date":"2017-07-26"}]

And i cant access to the index with $v->{"id"}, $v->id, $v["id"], i dont know if is for the way i save the information in dates, this is my function, thanks.

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {

        $properties = DB::table( 'properties' )->where( 'status', '=', 1 )->whereNotNull( 'id_client' )->whereNotNull( 'id_period' )->get();

        $dates = array();

        foreach ($properties as $k => $v) {
            $dates[$k] = DB::table( 'periods' )->where( 'id', '=', $v->id_period )->whereDate('date', '>', date('Y-m-d'))->get();
        }

        $properties_status = array();

        /* ----- HERE ---------*/
        foreach ($dates as $v) {
            Log::info($v);
        }


    })->everyMinute();
}

You should json_decode($v) so you can treat it like a struct. Otherwise it's just a string to you.

This is JSON. It's a kind of data format. You can decode it in PHP by using:

$var = json_decode($v, true);

And in $var you will get a normal array. Which you can simple check with print_r($var); .


Manual

PHP: json_decode

DB::table()->get()返回一个集合,因此可以使用:

Log::info($v->id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM