将分隔数组键解析为多维键路径

问题描述 投票:0回答:3

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

$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
显示孩子们是参考。有更好的办法吗?

php arrays multidimensional-array explode
3个回答
2
投票

你只是有一个错误的

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)
      }
    }
  }
}

0
投票

我认为这应该可行,传入你的(现有)数组,想要的键(为什么不使用点符号而不是'_|_'?),以及你想要的值

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;
    }

0
投票
<?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>';
?>
© www.soinside.com 2019 - 2024. All rights reserved.