拆分一个字符串,形成多维数组的钥匙? [英] Split a string to form multidimensional array keys?

查看:123
本文介绍了拆分一个字符串,形成多维数组的钥匙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建JSON途中从PHP数组codeD数据,可以是两个或三个层次的深度,这是这个样子:

I'm creating JSON encoded data from PHP arrays that can be two or three levels deep, that look something like this:

[grandParent] => Array (
                       [parent] => Array (
                                         [child] => myValue 
                                      )
                    )

的方法我有,这简直是在code手动创建嵌套数组需要我然而,通过打字了一些可怕的嵌套数组,用我的'的SetOption功能(即后来处理的编码):

The method I have, which is simply to create the nested array manually in the code requires me to use my 'setOption' function (which handles the encoding later) by typing out some horrible nested arrays, however:

$option = setOption("grandParent",array("parent"=>array("child"=>"myValue")));

我希望能够在这种情况下使用类似的表示法来javascript来得到相同的结果,因为我打算在多个页面中设置选项比较多,上面只是没有很好的可读性,尤其是当嵌套数组包含多个键 - 而能够做到这将使更多的意义:

I wanted to be able to get the same result by using similar notation to javascript in this instance, because I'm going to be setting many options in many pages and the above just isn't very readable, especially when the nested arrays contain multiple keys - whereas being able to do this would make much more sense:

$option = setOption("grandParent.parent.child","myValue");

任何人都可以提出一个方法能够通过分裂的字符串创建多维数组。这样我可以json_en code()它变成一个嵌套的对象?

Can anyone suggest a way to be able to create the multidimensional array by splitting the string on the '.' so that I can json_encode() it into a nested object?

(该功能的SetOption目的是一气呵成以后的编码在众人面前,收集所有的选项连成一个大的,嵌套的PHP数组,所以这就是解决方案会去)

(the setOption function purpose is to collect all of the options together into one large, nested PHP array before encoding them all in one go later, so that's where the solution would go)

编辑:我知道我可以在code做到这一点:

I realise I could do this in the code:

$options['grandparent']['parent']['child'] = "myValue1";
$options['grandparent']['parent']['child2'] = "myValue2";
$options['grandparent']['parent']['child3'] = "myValue3";

这可能是简单的;但建议将依然岩(因为我用它我作为更广泛的目标的一部分,所以它的 $ obj->的SetOption(键,值);

推荐答案

这应该如果他们尚未创建,并相应设置键填充子阵列为你($c$cpad

This ought to populate the sub-arrays for you if they haven't already been created and set keys accordingly (codepad here):

function set_opt(&$array_ptr, $key, $value) {

  $keys = explode('.', $key);

  // extract the last key
  $last_key = array_pop($keys);

  // walk/build the array to the specified key
  while ($arr_key = array_shift($keys)) {
    if (!array_key_exists($arr_key, $array_ptr)) {
      $array_ptr[$arr_key] = array();
    }
    $array_ptr = &$array_ptr[$arr_key];
  }

  // set the final key
  $array_ptr[$last_key] = $value;
}

说它像这样:

$opt_array = array();
$key = 'grandParent.parent.child';

set_opt($opt_array, $key, 'foobar');

print_r($opt_array);

在你的编辑保存,你可能会想适应这个以您的类中使用阵列 ...但希望这提供了一个地方开始!

In keeping with your edits, you'll probably want to adapt this to use an array within your class...but hopefully this provides a place to start!

这篇关于拆分一个字符串,形成多维数组的钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆