PHP 键名数组

发布于 2024-08-26 18:39:10 字数 605 浏览 3 评论 0原文

我有一个数组 $data

 fruit => apple,
 seat => sofa,

等。我想循环遍历,使每个键变成 type_key[0]['value'] 所以例如

 type_fruit[0]['value'] => apple,
 type_seat[0]['value'] => sofa,

我认为会做到这一点,即

foreach ($data as $key => $value)
{
        # Create a new, renamed, key. 
        $array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

        # Destroy the old key/value pair
        unset($array[$key]);

}

print_r($array);

不起作用。我怎样才能让它发挥作用?

另外,我希望键(而不是值)中的所有内容都是小写:是否也有一种简单的方法可以做到这一点?谢谢。

I have an array $data

 fruit => apple,
 seat => sofa,

etc. I want to loop through so that each key becomes type_key[0]['value'] so eg

 type_fruit[0]['value'] => apple,
 type_seat[0]['value'] => sofa,

and what I thought would do this, namely

foreach ($data as $key => $value)
{
        # Create a new, renamed, key. 
        $array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

        # Destroy the old key/value pair
        unset($array[$key]);

}

print_r($array);

Doesn't work. How can I make it work?

Also, I want everything to be in the keys (not the values) to be lowercase: is there an easy way of doing this too? Thanks.

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

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

发布评论

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

评论(4

绝影如岚 2024-09-02 18:39:10

您的意思是要将键放入单独的数组中吗?或者您的意思是只更改同一数组中的键?

    $array = array();
    foreach ($data as $key => $value)
    {
        $array['type_' . strtolower($key)] = array(array('value' => $value));
    }

如果您希望键是单独的变量,请执行以下操作:

    extract($array);

现在您将拥有 $type_fruit 和 $type_sofa。您可以在 $type_fruit[0]['value'] 中找到您的值,因为我们在其中放置了一个额外的嵌套数组。

Do you mean you want to make the keys into separate arrays? Or did you mean to just change the keys in the same array?

    $array = array();
    foreach ($data as $key => $value)
    {
        $array['type_' . strtolower($key)] = array(array('value' => $value));
    }

if you want your keys to be separate variables, then do this:

    extract($array);

Now you will have $type_fruit and $type_sofa. You can find your values as $type_fruit[0]['value'], since we put an extra nested array in there.

記柔刀 2024-09-02 18:39:10

你的要求听起来……很可疑。也许你的意思是这样的:

$arr = array('fruit' => 'apple', 'seat' => 'sofa');
$newarr = array();

foreach ($arr as $key => $value)
{
  $newkey = strtolower("type_$key");
  $newarr[$newkey] = array(array('value' => $value));
}

var_dump($newarr);

Your requirements sound... suspect. Perhaps you meant something like the following:

$arr = array('fruit' => 'apple', 'seat' => 'sofa');
$newarr = array();

foreach ($arr as $key => $value)
{
  $newkey = strtolower("type_$key");
  $newarr[$newkey] = array(array('value' => $value));
}

var_dump($newarr);
拍不死你 2024-09-02 18:39:10

首先,我不会改变输入数组,而是创建一个新的数组,除非您的内存限制遇到严重的问题。
然后,您不能简单地替换键来向数组添加更深的嵌套级别。
$x[ 'abc[def]' ] 仍然只引用顶级元素,因为 abc[def] 被解析为一个字符串,但您想要 $x['abc']['def']

$data = array(
 'fruit' => 'apple',
 'seat' => 'sofa'
);

$result = array();

foreach($data as $key=>$value) {
  $target = 'type_'.$key; 
  // this might be superfluous, but who knows... if you want to process more than one array this might be an issue.
  if ( !isset($result[$target]) || !is_array($result[$target]) ) {
    $result[$target] = array();
  }
  $result[$target][] = array('value'=>$value);
}

var_dump($result);

First I wouldn't alter the input-array but create a new one unless you're in serious, serious trouble with your memory limit.
And then you can't simply replace the key to add a deeper nested level to an array.
$x[ 'abc[def]' ] is still only referencing a top-level element, since abc[def] is parsed as one string, but you want $x['abc']['def'].

$data = array(
 'fruit' => 'apple',
 'seat' => 'sofa'
);

$result = array();

foreach($data as $key=>$value) {
  $target = 'type_'.$key; 
  // this might be superfluous, but who knows... if you want to process more than one array this might be an issue.
  if ( !isset($result[$target]) || !is_array($result[$target]) ) {
    $result[$target] = array();
  }
  $result[$target][] = array('value'=>$value);
}

var_dump($result);
恋竹姑娘 2024-09-02 18:39:10

对于初学者来说

$array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

应该是

$array[str_replace("/(.+)/", type_$1[0]['value'], $key)] = $value;

for starters

$array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

should be

$array[str_replace("/(.+)/", type_$1[0]['value'], $key)] = $value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文