从字符串创建数组

发布于 2024-09-25 01:35:00 字数 185 浏览 1 评论 0原文

我需要创建这样的数组:

Array('firstkey' => Array('secondkey' => Array('nkey' => ...)))

从此:

firstkey.secondkey.nkey.(...)

I need to create array like that:

Array('firstkey' => Array('secondkey' => Array('nkey' => ...)))

From this:

firstkey.secondkey.nkey.(...)

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

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

发布评论

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

评论(3

小梨窩很甜 2024-10-02 01:35:00
$yourString = 'firstkey.secondkey.nkey';
// split the string into pieces
$pieces = explode('.', $yourString);
$result = array();
// $current is a reference to the array in which new elements should be added
$current = &$result;
foreach($pieces as $key){
   // add an empty array to the current array
   $current[ $key ] = array();
   // descend into the new array
   $current = &$current[ $key ];
}
//$result contains the array you want
$yourString = 'firstkey.secondkey.nkey';
// split the string into pieces
$pieces = explode('.', $yourString);
$result = array();
// $current is a reference to the array in which new elements should be added
$current = &$result;
foreach($pieces as $key){
   // add an empty array to the current array
   $current[ $key ] = array();
   // descend into the new array
   $current = &$current[ $key ];
}
//$result contains the array you want
近箐 2024-10-02 01:35:00

我对此的看法:

<?php

$theString = "var1.var2.var3.var4";
$theArray = explode(".", $theString); // explode the string into an array, split by "." 
$result =  array();
$current = &$result; // $current is a reference to the array we want to put a new key into, shifting further up the tree every time we add an array.

while ($key = array_shift($theArray)){ // array_shift() removes the first element of the array, and assigns it to $key. The loop will stop as soon as there are no more items in $theArray.
    $current[$key] = array(); // add the new key => value pair
    $current = &$current[$key]; // set the reference point to the newly created array.
}

var_dump($result);

?>

在 while 循环中使用 array_push()

您希望最深的密钥的值是多少?

My take on this:

<?php

$theString = "var1.var2.var3.var4";
$theArray = explode(".", $theString); // explode the string into an array, split by "." 
$result =  array();
$current = &$result; // $current is a reference to the array we want to put a new key into, shifting further up the tree every time we add an array.

while ($key = array_shift($theArray)){ // array_shift() removes the first element of the array, and assigns it to $key. The loop will stop as soon as there are no more items in $theArray.
    $current[$key] = array(); // add the new key => value pair
    $current = &$current[$key]; // set the reference point to the newly created array.
}

var_dump($result);

?>

With an array_push() in the while loop.

What do you want the value of the deepest key to be?

时光匆匆的小流年 2024-10-02 01:35:00

尝试类似的方法:

function dotToArray() {
  $result = array();
  $keys = explode(".", $str);
  while (count($keys) > 0) {
    $current = array_pop($keys);
    $result = array($current=>$result);
  }
  return $result;
}

Try something like:

function dotToArray() {
  $result = array();
  $keys = explode(".", $str);
  while (count($keys) > 0) {
    $current = array_pop($keys);
    $result = array($current=>$result);
  }
  return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文