我知道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);更多推荐
发布评论