douju5062 2012-06-11 12:57
浏览 32
已采纳

使自定义php“爆炸”函数递归

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);
}
  • 写回答

1条回答 默认 最新

  • dqr3883 2012-06-11 14:16
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c