Skip to content

Instantly share code, notes, and snippets.

@pthiers
Created October 23, 2017 14:05
Show Gist options
  • Save pthiers/050e96f1b20cd3f17ac540424a2ef2b7 to your computer and use it in GitHub Desktop.
Save pthiers/050e96f1b20cd3f17ac540424a2ef2b7 to your computer and use it in GitHub Desktop.
Dynamic column mariadb array to column_create
namespace App\Utils;
use Illuminate\Support\Collection;
class MariaDB
{
/**
* @param $arr array|Collection
* @return string
*/
public static function arrayToColumnCreate($arr){
if($arr instanceof Collection)
$arr = $arr->toArray();
$return = [];
foreach ($arr as $key => $value){
$return[] = "'".trim($key,"'")."'";
if(is_array($value) || $value instanceof Collection){
$return[] = self::arrayToColumnCreate($value);
}elseif(is_integer($value)){
$return[] = (int) $value;
}else{
$return[] = "'".trim($value,"'")."'";
}
}
return empty($return) ?'null' : "column_create(".implode(',',$return).")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment