英文:
How can I turn this PHP numeric array into an array of objects with specific properties?
问题
我正在开发一个基于 Laravel 8 的博客应用程序。
该应用程序支持主题。简而言之,主题支持的工作方式如下:
在 views
目录中,我有包含每个主题视图的目录。
在 public\themes
中,我保存了每个主题的资产(CSS、JavaScript)。
在 SettingsController 控制器 中,我有一个变量名为 $theme_directory
,其中包含当前主题的目录名称。我在主题视图中使用这个变量,例如:
<link href="{{ asset('themes/' . $theme_directory . '/css/clean-blog.min.css') }}" rel="stylesheet">
从“设置”部分,我可以设置当前主题。
我现在正试图用一个下拉框来替代我输入主题(目录)名称的文本字段。为此,我有:
public function themes() {
$themes = [];
$themes_path = Config::get('view.paths')[0] . '\themes';
foreach(File::directories($themes_path) as $theme) {
array_push($themes, array_reverse(explode('\\', $theme))[0]);
}
return $themes;
}
我将主题数组传递到视图中:
public function index() {
$settings = Settings::first();
return view('dashboard/settings', [
'settings' => $settings,
'themes' => $this->themes()
]);
}
上述方法返回一个类似于以下的数组:
[
0 => "calvin",
1 => "clean-blog",
]
目标
我需要将其转换为对象数组,每个数组成员都有 slug("clean-blog")和 name("Clean blog")属性,以便我可以在视图(表单)中像这样使用:
<select name="theme_directory" id="theme" class="form-control @error('theme_directory') is-invalid @enderror">
<option value="">选择一个主题</option>
@foreach($themes as $theme)
<option value="{{ $theme->slug }}" {{ $theme->slug == $settings->theme_directory ? 'selected' : '' }}>{{ $theme->name }}</option>
@endforeach
</select>
我该如何实现这一目标?
英文:
I am working on a blogging application in Laravel 8.
The application supports themes. In a nutshell, theme support works like this:
In the views
directory, I have the directory containing the views of every theme.
In public\themes
I keep the assets (CSS, Javascript) of every theme.
In the SettingsController controller I have, among others, a variable named $theme_directory, which contains the name of the directory of the current theme. I use this variable in the theme view, like this, for instance:
<link href="{{ asset('themes/' . $theme_directory . '/css/clean-blog.min.css') }}" rel="stylesheet">
From the "Settings" section I can set the current theme.
I am now trying to replace the text field where I type the theme's (directory) name with a select-box. For this purpose I have:
public function themes() {
$themes = [];
$themes_path = Config::get('view.paths')[0] . '\themes';
foreach(File::directories($themes_path) as $theme) {
array_push($themes, array_reverse(explode('\\', $theme))[0]);
}
return $themes;
}
I pass the arary of themes to the view:
public function index() {
$settings = Settings::first();
return view('dashboard/settings', [
'settings' => $settings,
'themes' => $this->themes()
]);
}
The above method returns an array like this:
[▼
0 => "calvin"
1 => "clean-blog"
]
The goal
I need to turn that into am array of objects instead, with the properties slug ("clean-blog") and name ("Clean blog") for each member of the array above, so that I can use it like this in the view (form):
<select name="theme_directory" id="theme" class="form-control @error('theme_directory') is-invalid @enderror">
<option value="">Pick a theme</option>
@foreach($themes as $theme)
<option value="{{ $theme->slug }}" {{ $theme->slug == $settings->theme_directory ? 'selected' : '' }}>{{ $theme->name }}</option>
@endforeach
</select>
How can I achieve that?
答案1
得分: 0
只需通过将短横线替换为空格,然后将第一个字母大写(使用 ucfirst 函数)来将您的 slug 转换为名称。
然后将其放入关联数组中(我在这里使用 compact 函数来简化)。
public function themes() {
$themes = [];
$themes_path = Config::get('view.paths')[0] . '/themes';
foreach(File::directories($themes_path) as $theme) {
$slug = array_reverse(explode('\\', $theme))[0];
$name = ucwords(str_replace('-', ' ', $slug));
$themes[] = (object)compact('slug', 'name');
}
return $themes;
}
英文:
Just transform your slug into a name by replacing dashes by spaces, then upper case the first lerter (ucfirst function).
Then put into an associative array (I'm using compact functiom here to simplify)
public function themes() {
$themes = [];
$themes_path = Config::get('view.paths')[0] . '\themes';
foreach(File::directories($themes_path) as $theme) {
$slug = array_reverse(explode('\\', $theme))[0];
$name = ucwords(str_replace('-', ' ', $slug));
$themes[] = (object)compact('slug', 'name');
}
return $themes;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论