使自定义php“爆炸”函数递归(Making a custom php “explode” function recursive)

编程入门 行业动态 更新时间:2024-10-25 10:26:33
使自定义php“爆炸”函数递归(Making a custom php “explode” function recursive)

我知道http_build_query()和parse_str()是解决这类问题的更好的解决方案,但是我无法改变我正在处理的特定项目中的数据结构,所以我试图通过创建来简化一些事情来自数据库的大型3d关联数组存储的字符串。

有人可以帮我递一下这个函数递归吗? 我想要它采取这样的事情:

explode3D( $separators = array('|',','), $string = "val1,val2|val1,val2,val3", $keys = "cat1,item1,item2|cat2,item1,item2,item3" );

并返回这样的东西

Array ( 'cat1' => Array('item1'=>'val1', 'item2'=>'val2'), 'cat2' => Array('item1'=>'val1', 'item2'=>'val2', 'item3'=>'val3') )

这是迄今为止的功能:

function explode3D($separators,$string,$keys=0){ $res = array(); if(is_array($separators)){//MULTI DIMENSION MODE: (unfinished!!) //help needed here// }else{//SINGLE DIMENSION MODE: $vals = explode($separators,$string); if($keys === 0){ //NO KEYS: $res = $vals; }else if($keys === 1){//ALTERNATE ROWS ARE KEYS: $key = ''; for($i=0; $i<sizeof($vals); $i++){ if ($i++ % 2 == 1 ){ //every second element: $key = $vals[$i]; //save the key }else{ $res[$key] = $vals[$i]; //set the saved key = value } } }else{ //GET KEYS FROM $keys ARRAY: if(is_string($keys)){ //explode keys string if necessary (using $separators) $keys = explode3D($separators,$keys); } for($i=0; $i<sizeof($vals); $i++){ $res[$keys[$i]] = $vals[$i]; } } } return($res); }

I know http_build_query() and parse_str() are a better solution for this sort of problem but I can't change the structure of the data in the particular project I'm working on so I'm trying to simplify things a little by creating large 3d associative arrays from a database stored string.

Can someone please give me a hand making this function recursive? I want it to take something like this:

explode3D( $separators = array('|',','), $string = "val1,val2|val1,val2,val3", $keys = "cat1,item1,item2|cat2,item1,item2,item3" );

And return something like this

Array ( 'cat1' => Array('item1'=>'val1', 'item2'=>'val2'), 'cat2' => Array('item1'=>'val1', 'item2'=>'val2', 'item3'=>'val3') )

Here's the function so far:

function explode3D($separators,$string,$keys=0){ $res = array(); if(is_array($separators)){//MULTI DIMENSION MODE: (unfinished!!) //help needed here// }else{//SINGLE DIMENSION MODE: $vals = explode($separators,$string); if($keys === 0){ //NO KEYS: $res = $vals; }else if($keys === 1){//ALTERNATE ROWS ARE KEYS: $key = ''; for($i=0; $i<sizeof($vals); $i++){ if ($i++ % 2 == 1 ){ //every second element: $key = $vals[$i]; //save the key }else{ $res[$key] = $vals[$i]; //set the saved key = value } } }else{ //GET KEYS FROM $keys ARRAY: if(is_string($keys)){ //explode keys string if necessary (using $separators) $keys = explode3D($separators,$keys); } for($i=0; $i<sizeof($vals); $i++){ $res[$keys[$i]] = $vals[$i]; } } } return($res); }

最满意答案

以下是它应该如何工作的想法。 我相信很少有错别字和错过的操作符,但它有助于你理解操作方法。

<?php $string = "val1,val2|val1,val2,val3"; $keys = "cat1,item1,item2|cat2,item1,item2,item3"; $firstDimension = explode("|", $keys); $firstDimensionValues = explode("|", $string); $newArray = Array(); for($i = 0; $i < count($firstDimension); $i++) { $d2Keys = explode(",", $firstDimension[$i]); $key = $d2Keys[0]; array_shift($d2Keys); // remove $key $d2Values = explode(",", $firstDimensionValues[$i]); for ($a = 0; $a < count($d2Keys); $a++) { $newArray[$key][$d2Keys[$a]] = $d2Values[$a]; } } print_r($newArray);

Here's the idea how it supposed to work. I believe there are few typos and missed operators, but it hopefully helps you to understand how-to.

<?php $string = "val1,val2|val1,val2,val3"; $keys = "cat1,item1,item2|cat2,item1,item2,item3"; $firstDimension = explode("|", $keys); $firstDimensionValues = explode("|", $string); $newArray = Array(); for($i = 0; $i < count($firstDimension); $i++) { $d2Keys = explode(",", $firstDimension[$i]); $key = $d2Keys[0]; array_shift($d2Keys); // remove $key $d2Values = explode(",", $firstDimensionValues[$i]); for ($a = 0; $a < count($d2Keys); $a++) { $newArray[$key][$d2Keys[$a]] = $d2Values[$a]; } } print_r($newArray);

更多推荐

本文发布于:2023-07-23 18:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   自定义   函数   php   Making

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!