PHP字符串到多维数组



假设我的变量中有以下字符串:

$arr['data_|_multilanguage_|_en_|_field'] = 23;

我想把它转换成一个多维数组,如下所示:

$arr['data']['multilanguage']['en']['field'] = 23;

我做了一个递归函数

foreach ($arr as $outer_key => $outer_value) {
    $parts = explode('_|_', $outer_key);
    $reference =& $arr;
    $final_key = count($parts) - 1;
    foreach ($parts as $key => $part) {
        if (!$final_key == $key) {
            if (empty($reference[$part])) {
                $reference[$part] = array();
            }
            $reference =& $reference[$part];
        } else {
            $reference[$part] = $arr[$str];
            unset($arr[$outer_key]);
        }
    }
}

问题是var_dump显示子节点是引用的。有没有更好的办法?

您只是有一个错误的if()语句:

 if (!$final_key == $key) {}

!将在 ==之前计算
改成:

 if ($final_key != $key) {}

(你在最后的$reference[$part] =作业中有另一个小问题,我在下面的代码中纠正了它)

$arr['data_|_multilanguage_|_en_|_field'] = 23;
foreach ($arr as $outer_key => $outer_value) {
    $parts = explode('_|_', $outer_key);
    $reference =& $arr;
    $final_key = count($parts) - 1;
    foreach ($parts as $key => $part) {
        if ($final_key != $key) { 
            if (empty($reference[$part])) {
                $reference[$part] = array();
            }
            $reference =& $reference[$part];
        } else {
            $reference[$part] = $outer_value;
            unset($arr[$outer_key]);
        }
    }
}

这是你的结果

array(1) {
  ["data"]=>
  array(1) {
    ["multilanguage"]=>
    array(1) {
      ["en"]=>
      &array(1) {
        ["field"]=>
        int(23)
      }
    }
  }
}

我认为这应该工作,传递你的(现有的)数组,想要的键(为什么不使用点符号代替'_|_' ?),和你想要的值

function array_set(&$array, $key, $value)
    {
        if (is_null($key)) return $array = $value;
        $keys = explode('_|_', $key);
        while (count($keys) > 1)
        {
            $key = array_shift($keys);
            // If the key doesn't exist at this depth, we will just create an empty array
            // to hold the next value, allowing us to create the arrays to hold final
            // values at the correct depth. Then we'll keep digging into the array.
            if ( ! isset($array[$key]) || ! is_array($array[$key]))
            {
                $array[$key] = array();
            }
            $array =& $array[$key];
        }
        $array[array_shift($keys)] = $value;
        return $array;
    }
<?php
$arr = array();
$arr['data_|_multilanguage_|_en_|_field'] = 23;
$arr['data_|_multilanguage_|_en_|_field2'] = 32;
$arr['data_|_multilanguage_|_fr_|_field'] = 57;
$arr['data_|_test'] = 57;
$arr['data_|_testing_|_var'] = 57;
function doArray(&$newarr,$keys,$value) {
    if(count($keys) > 1) {
        $key = array_shift($keys);
        if(!isset($newarr[$key]) || !is_array($newarr[$key])) {
            $newarr[$key] = array();
        }
        doArray($newarr[$key],$keys,$value);
    } else {
        $newarr[array_shift($keys)] = $value;
    }
}
$newarr = array();
foreach($arr AS $key => $value) {
    $keys = explode("_|_",$key);
    doArray($newarr,$keys,$value);
}
echo '<pre>';
print_r($newarr);
echo '</pre>';
?>

相关内容

  • 没有找到相关文章