将点分隔成数组键的字符串转换为数组键

发布于 2024-11-13 02:26:24 字数 492 浏览 2 评论 0原文

我有一个字符串(例如 one.two. Threemonth.2),我需要将此字符串爆炸点字符转换为数组键。

所以我有 month.2 并且我需要代码线将此字符串转换为 $lang['month'][2]

我一直在寻找该解决方案,但我不是能找到吗,我被屏蔽了。


我终于找到了这个:

<?php
$lang['one']['two']['three'] = 'well done';

$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
    $result .= '["'.$item.'"]';
}

var_dump(eval("return " . $result.';'));
?>

I have an string (like one.two.three or month.2) and I need to translate this string exploding point character into array keys.

So I have month.2 and I need the codeline to translate this string into $lang['month'][2]

I was looking for that solution but I am not able to find it, I am blocked.


I finally found this:

<?php
$lang['one']['two']['three'] = 'well done';

$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
    $result .= '["'.$item.'"]';
}

var_dump(eval("return " . $result.';'));
?>

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

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

发布评论

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

评论(3

终止放荡 2024-11-20 02:26:24

首先分解它:

$in = explode('.', $in);

然后将其重写为嵌套数组:

$arg = array();
foreach (array_reverse($in) as $key) {
   $arg = array($key => $arg);
}

最后合并:

$out = array_merge_recursive($arg, $out);

编辑:如果您想要读取数组,那么 bazmegakapa 的答案就是您正在寻找的。

Start by exploding it:

$in = explode('.', $in);

Then rewrite it as a nested array:

$arg = array();
foreach (array_reverse($in) as $key) {
   $arg = array($key => $arg);
}

And finally merge:

$out = array_merge_recursive($arg, $out);

Edit: in case you're seeking to read the array instead, then bazmegakapa's answer is what you're looking for.

帅的被狗咬 2024-11-20 02:26:24

我写了一个小递归函数来处理这个问题。首先,分解字符串,并将数组(在您的示例中为 $lang,在我的示例中为 $a)和此分解数组传递给 GetVal()。它会尽力将所需的值返回到$x中。

$s="month.2";
$keys=explode('.', $s);
$a=array(
    'month' => array('1' => 'fos', '2' => 'fos2'),
    'retek' => 1
);

function GetVal($array, $keyarray) {
    $key=array_shift($keyarray);
    if (array_key_exists($key, $array)) {
        if (count($keyarray)==0) {
            return $array[$key];
        } else {
            return GetVal($array[$key], $keyarray);
        }
    } else {
        return null;
    }
}

$x=GetVal($a, $keys);
var_dump($x);

I wrote a little recursive function to handle this. First you explode the string, and pass the array ($lang in your example, $a in mine) and this exploded array to GetVal(). It will do its best to return the needed value into $x.

$s="month.2";
$keys=explode('.', $s);
$a=array(
    'month' => array('1' => 'fos', '2' => 'fos2'),
    'retek' => 1
);

function GetVal($array, $keyarray) {
    $key=array_shift($keyarray);
    if (array_key_exists($key, $array)) {
        if (count($keyarray)==0) {
            return $array[$key];
        } else {
            return GetVal($array[$key], $keyarray);
        }
    } else {
        return null;
    }
}

$x=GetVal($a, $keys);
var_dump($x);
初见终念 2024-11-20 02:26:24

这是对explode函数使用的澄清:

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

这段代码爆炸为:

Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)

not as Array[hello][world][its][a].......

Its a clarification for the use of explode function:

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>

this code explodes as:

Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)

not as Array[hello][world][its][a].......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文