简体   繁体   English

从数组Laravel PHP获取数组值

[英]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. 我在laravel中进行计划任务,我需要知道其他表的值,在这种情况下,我会获取属性,后来我从表属性中的外键将句点保存在数组中,以后我需要从在表格周期中的“日期”,但是当我得到日志时,我得到了这个。

[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. 而且我无法使用$ v-> {“ id”},$ v-> id,$ v [“ id”]访问索引,我不知道这是否是用于保存日期信息的方式,这是我的功能,谢谢。

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. 您应该使用json_decode($v)以便将其视为结构。 Otherwise it's just a string to you. 否则对您来说只是一个字符串。

This is JSON. 这是JSON。 It's a kind of data format. 这是一种数据格式。 You can decode it in PHP by using: 您可以使用以下方法在PHP中对其进行解码:

$var = json_decode($v, true);

And in $var you will get a normal array. $var您将获得一个普通数组。 Which you can simple check with print_r($var); 您可以使用print_r($var);简单检查print_r($var); .


Manual 手册

PHP: json_decode PHP:json_decode

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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