如何将同一数组中的一个键值与其他键值进行匹配?

发布于 2025-01-02 16:07:21 字数 620 浏览 2 评论 0原文

我很抱歉,因为我知道这可能是一个菜鸟问题,但我有太多的工作要做,所以我无法完成我的 PHP 学习过程。

假设我有一个用于文件扩展名的变量和一个 PHP 数组,如下所示:

$fileExt = ".php";

$mainNav = array(

    "page01" => array (

        "id"    =>  "value",
        "name"  =>  "value",
        "title" =>  "value",
        "url"   =>  need same "id" value + $fileExt

    ),

    "page02" => array (

        "id"    =>  "value",
        "name"  =>  "value",
        "title" =>  "value",
        "url"   =>  need same "id" value + $fileExt

    ),
);

但我喜欢这样,而不是对“url”的值进行硬编码,它与键“name”的值+我已经存在的文件扩展名相匹配有一个变量,以防万一我以后需要更改它。

I'm sorry because i know that is probably a noob question, but i have so much work to do that i cant finish my PHP learning process.

Let's say i have a variable for the file extension and an array in PHP like that:

$fileExt = ".php";

$mainNav = array(

    "page01" => array (

        "id"    =>  "value",
        "name"  =>  "value",
        "title" =>  "value",
        "url"   =>  need same "id" value + $fileExt

    ),

    "page02" => array (

        "id"    =>  "value",
        "name"  =>  "value",
        "title" =>  "value",
        "url"   =>  need same "id" value + $fileExt

    ),
);

But I like that instead of hard coding the value for "url", it match the value of the key "name" + the file extension that i already have in a variable just in case i need to change it later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2025-01-09 16:07:21

这应该可以完成这项工作:

$fileExt = ".php";

$var['page01'] => array (
    "id"    =>  "inicio",
    "name"  =>  "Inicio",
    "title" =>  "Incio - Cerartec",
);

$var['page01']['url'] = $var['page01']['name'] . $fileExt;

This should do the job:

$fileExt = ".php";

$var['page01'] => array (
    "id"    =>  "inicio",
    "name"  =>  "Inicio",
    "title" =>  "Incio - Cerartec",
);

$var['page01']['url'] = $var['page01']['name'] . $fileExt;
方圜几里 2025-01-09 16:07:21

有几种方法可以实现此目的,下面可能是最简单的方法,无需查看您正在使用的所有代码。

$fileExt = ".php";

$mainNav = array(

   "page01" => array (
      "id"    =>  "idvalue",
      "name"  =>  "value",
      "title" =>  "value"
   ),

   "page02" => array (
      "id"    =>  "idvalue2",
      "name"  =>  "value",
      "title" =>  "value"
   )
);

foreach($mainNav as $key => $value)
{
   $mainNav[$key]["url"] = $value["id"] . $fileExt;
}

我也在想这些东西是存储在数据库中的吗?如果是这样,只需为 url 添加一个字段,当您插入页面时,请同时执行 url 操作。

There's a few ways to accomplish this, below is probably the easiest without seeing all the code you are using.

$fileExt = ".php";

$mainNav = array(

   "page01" => array (
      "id"    =>  "idvalue",
      "name"  =>  "value",
      "title" =>  "value"
   ),

   "page02" => array (
      "id"    =>  "idvalue2",
      "name"  =>  "value",
      "title" =>  "value"
   )
);

foreach($mainNav as $key => $value)
{
   $mainNav[$key]["url"] = $value["id"] . $fileExt;
}

I'm also thinking that this stuff is being stored in a database? If so just add a field for the url and when you insert the page go ahead and do the url at the same time.

淡墨 2025-01-09 16:07:21
$url = $page01['url'].$fileExt;
$url = $page01['url'].$fileExt;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文