PHP字符串嵌套/多维数组 [英] PHP string to nested / multidimensional array
问题描述
我有这样的例子PHP字符串:
$字符串=@ [ITEM_1] [门] @ [莫扎特] [草] = YES @ [莫扎特] [绿] =无@ [莫扎特] [人] @ [蓝] [电影] = YES @ ITEM_1] [节拍] = YES @ [ITEM_1] [音乐] =无
块引用>现在$字符串idented只是易观:
- @ [ITEM_1] [门]结果
- @ [莫扎特] [草] = YES结果
- @ [莫扎特] [绿] =无结果
- @ [莫扎特] [人]搜索结果
- @ [蓝] [电影] = YES搜索结果
- @ [ITEM_1] [节拍] = YES结果
- @ [ITEM_1] [音乐] =无结果
块引用>我想知道我怎么能得到这个字符串(或以下这种风格等字符串),并在一个数组,看起来像变换:
阵列
(
[ITEM_1] =>排列
(
[门] =>排列
(
[莫扎特] =>排列
(
[草] =>是
[绿色] =>没有
[人] =>排列
(
[蓝] =>排列
(
[电影] =>是
)
)
)
)
[拍] =>是
[音乐] =>没有
)
)我想什么
我试图使用和递归函数创建一个嵌套数组,但我不能在递归函数访问数组指针(深层次)..不知道为什么..也许是错误的补丁,回答。
谢谢你,解决方案好吧,我希望你还是需要这个,因为我浪费了比我更想联系得到这个正确的时间:)
基本上,我的做法是首先操纵[的]串入格式[设置] [键] =值,然后通过钥匙串回路,并与最后一组按键比较他们创造正确的密钥层次结构。我用的eval因为它更容易,但你可以写一个替换功能,如果你不能忍受看到在code,它的功能:
//首先我们得到的字符串转换成更易于使用CHUNKS WORK
$ original_string =@ [ITEM_1] [门] @ [莫扎特] [草] = YES @ [莫扎特] [绿] =无@ [莫扎特] [人] @ [蓝] [电影] = YES @ [ITEM_1] [击败] = YES @ [ITEM_1] [音乐] =无;
$ cleaned_string = str_replace函数('] @ [','] [',$ original_string);
/ *这导致等于值按键集群:
@ [ITEM_1] [门] [莫扎特] [草] = YES @ [莫扎特] [绿] =无@ [莫扎特] [人] [蓝] [电影] = YES @ [ITEM_1] [击败] = YES @ ITEM_1] [音乐] =无OR(与换行符为清楚起见):@ [ITEM_1] [门] [莫扎特] [草] = YES
@ [莫扎特] [绿] =无
@ [莫扎特] [人] [蓝] [电影] = YES
@ [ITEM_1] [击败] = YES
@ [ITEM_1] [音乐] =无* ///把它分解成一个数组:
$元素=爆炸('@',$ cleaned_string);//创建一个变量最后一个字符串比较
$ last_keys =;
//而另一个将作为我们的最终数组
$ array_of_arrays =阵列();
//现在通过每个[ITEM_1] [门] [莫扎特] [草] = YES,[莫扎特] [绿] =无等循环
的foreach($元素作为$元素){
如果($元素==){继续;} //跳过第一个空项 //打破串入[0] =组键和[1]的值并因此终止了串
//所以[ITEM_1] [门] [莫扎特] [草] = YES变为[ITEM_1] [门] [莫扎特] [草],是的
$件=爆炸(=,str_replace函数(阵列([,]),阵列([',']),修整($元件)));
//现在这组按键比较最后一组密钥,如果它们重叠,将它们合并成一个单一的密钥字符串
$ clean_keys = combine_key_strings($件[0],$ last_keys);
//设置新的密钥字符串值下一个比较
$ last_keys = $ clean_keys;
//和(丑,我知道),我们使用一个eval转换[ITEM_1] [门] [莫扎特] [草] ='是'到正确键控阵列
的eval(\\ $ array_of_arrays$ clean_keys='.trim($件[1])。';);
}//现在转储内容
的print_r($ array_of_arrays);
//这个函数COMPA
功能combine_key_strings($新,老$){
//获取启动新的字符串键
$ new_keys =爆炸('] [',$新);
$ first_key = $ new_keys [0]']'。 //看它是否出现在最后一个字符串中
$ last_occurance = strrpos($岁,$ first_key);
//如果是这样,合并两个字符串创建完整KeyString中数组
如果(is_int($ last_occurance)){
返回SUBSTR($老,0,$ last_occurance)$新。
}
返回$新;
}这应该吐出你的正确嵌套数组:
阵列
(
[ITEM_1] =>排列
(
[门] =>排列
(
[莫扎特] =>排列
(
[草] =>是
[绿色] =>没有
[人] =>排列
(
[蓝] =>排列
(
[电影] =>是
) ) ) ) [拍] =>是
[音乐] =>没有
))晚安!
I have this example php string:
$string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no ";
now $string idented just to easy view:
- @[item_1][door]
- @[mozart][grass] = yes
- @[mozart][green] = no
- @[mozart][human]
- @[blue][movie]=yes
- @[item_1][beat] = yes
- @[item_1][music] = no
I want to know how can i get this string ( or other string following this style ) and transform in an array that looks like:
Array ( [item_1] => Array ( [door] => Array ( [mozart] => Array ( [grass] => yes [green] => no [human] => Array ( [blue] => Array ( [movie] => yes ) ) ) ) [beat] => yes [music] => no ) )
What i tried
I tried to use and recursive function to create an nested array but i can't have access to the array pointer ( in deep levels ) in recursive functions.. don't know why.. maybe is the wrong patch to the answer. thank you,
解决方案OK, I hope you still need this, because I wasted more time than I'd like to admin getting this right :)
Basically, my approach was to first manipulate the string into the format [set][of][keys]=value, and then loop through the string of keys and comparing them with the last set of keys to create the correct key hierarchy. I used eval because it's easier, but you can write a replacement function if you can't stomach seeing that function in your code:
//FIRST WE GET THE STRING INTO EASIER TO WORK WITH CHUNKS $original_string = "@[item_1][door] @[mozart][grass] = yes @[mozart][green] = no @[mozart][human] @[blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no "; $cleaned_string = str_replace('] @[','][',$original_string); /* This results in clusters of keys that equal a value: @[item_1][door][mozart][grass] = yes @[mozart][green] = no @[mozart][human][blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no OR (with line breaks for clarity): @[item_1][door][mozart][grass] = yes @[mozart][green] = no @[mozart][human][blue][movie]=yes @[item_1][beat] = yes @[item_1][music] = no */ //break it up into an array: $elements = explode('@',$cleaned_string); //create a variable to compare the last string to $last_keys = ""; //and another that will serve as our final array $array_of_arrays = array(); //now loop through each [item_1][door][mozart][grass] = yes,[mozart][green] = no, etc foreach($elements as $element){ if ($element==""){continue;} //skip the first empty item //break the string into [0] = group of keys and [1] the value that terminates the string //so [item_1][door][mozart][grass] = yes BECOMES [item_1][door][mozart][grass], AND yes $pieces = explode('=',str_replace(array('[',']'),array("['","']"),trim($element))); //now compare this set of keys to the last set of keys, and if they overlap merge them into a single key string $clean_keys = combine_key_strings($pieces[0],$last_keys); //set the new key string the value for the next comparison $last_keys = $clean_keys; //and (ugly, I know) we use an eval to convert "[item_1][door][mozart][grass]='yes'" into a properly keyed array eval("\$array_of_arrays".$clean_keys." = '".trim($pieces[1])."';"); } //now dump the contents print_r($array_of_arrays); //THIS FUNCTION COMPA function combine_key_strings($new,$old){ //get the key that starts the newer string $new_keys = explode('][',$new); $first_key = $new_keys[0].']'; //see if it appears in the last string $last_occurance = strrpos ($old,$first_key); //if so, merge the two strings to create the full array keystring if (is_int($last_occurance)){ return substr($old,0,$last_occurance).$new; } return $new; }
This should spit out your correctly nested array:
Array ( [item_1] => Array ( [door] => Array ( [mozart] => Array ( [grass] => yes [green] => no [human] => Array ( [blue] => Array ( [movie] => yes ) ) ) ) [beat] => yes [music] => no ) )
Good night!
这篇关于PHP字符串嵌套/多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!