如何将这个 PHP 数字数组转换为具有特定属性的对象数组?

huangapple go评论81阅读模式
英文:

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.

如何将这个 PHP 数字数组转换为具有特定属性的对象数组?

In public\themes I keep the assets (CSS, Javascript) of every theme.

如何将这个 PHP 数字数组转换为具有特定属性的对象数组?

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:

&lt;link href=&quot;{{ asset(&#39;themes/&#39; . $theme_directory . &#39;/css/clean-blog.min.css&#39;) }}&quot; rel=&quot;stylesheet&quot;&gt;	

From the "Settings" section I can set the current theme.

如何将这个 PHP 数字数组转换为具有特定属性的对象数组?

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(&#39;view.paths&#39;)[0] . &#39;\themes&#39;;
	foreach(File::directories($themes_path) as $theme) {
		array_push($themes, array_reverse(explode(&#39;\\&#39;, $theme))[0]);
	}
	return $themes;
}

I pass the arary of themes to the view:

public function index() {
  $settings = Settings::first();
  return view(&#39;dashboard/settings&#39;, [
    &#39;settings&#39; =&gt; $settings, 
    &#39;themes&#39; =&gt; $this-&gt;themes()
  ]);
} 

The above method returns an array like this:

[▼
	0 =&gt; &quot;calvin&quot;
	1 =&gt; &quot;clean-blog&quot;
]

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):

&lt;select name=&quot;theme_directory&quot; id=&quot;theme&quot; class=&quot;form-control @error(&#39;theme_directory&#39;) is-invalid @enderror&quot;&gt;
          &lt;option value=&quot;&quot;&gt;Pick a theme&lt;/option&gt;
          @foreach($themes as $theme)
          &lt;option value=&quot;{{ $theme-&gt;slug }}&quot; {{ $theme-&gt;slug == $settings-&gt;theme_directory  ? &#39;selected&#39; : &#39;&#39; }}&gt;{{ $theme-&gt;name }}&lt;/option&gt;
          @endforeach
&lt;/select&gt;

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(&#39;view.paths&#39;)[0] . &#39;\themes&#39;;
    foreach(File::directories($themes_path) as $theme) {
        $slug = array_reverse(explode(&#39;\\&#39;, $theme))[0];
        $name = ucwords(str_replace(&#39;-&#39;, &#39; &#39;, $slug));
        $themes[] = (object)compact(&#39;slug&#39;, &#39;name&#39;);
    }

    return $themes;
} 

huangapple
  • 本文由 发表于 2023年1月9日 03:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定