Search code examples
phparrayslaravelsubstr

is there a way to substring an array?


i want to substring an array given by the query

CONTROLLER (EX. value (Year_Report_2019, Year_Report_2020)

 $store=array();

 $tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");

    foreach($tables as $this){

     $get= substr($this,-4);

     $store[] = $get;
   }

BLADE want show result of (2019,2020)

{{implode(",", $store)}}

ERROR

substr() expects parameter 1 to be string, object given

Solution

  • You parsed your result wrong. (avoid using variable name $this)

    $store=array();
    
    $tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");
    
    foreach($tables as $table){
        $store[] = substr($table->Tables_in_database,-4);
    }