【问题标题】:How to create two arrays from String in PHP for json output如何在 PHP 中从字符串创建两个数组以用于 json 输出
【发布时间】:2015-03-13 10:45:23
【问题描述】:

我想在 MenuTable 和 SubMenuTable 中触发查询(在循环中)。

但我有数据是字符串,像这样:

"102_264,102_273,104_225,103_233,103_229,103_232,101_316,101_244,101_246,101_249"

这里102_264 (menuId_subMenuId)

  • 102menuId
  • 264subMenuId

所以,我想分离这个字符串,然后在上面的表中触发查询。 我该怎么做?

【问题讨论】:

  • explode by ,,循环,explode by _

标签: php json database web-services


【解决方案1】:

您可以使用 explode 函数或使用列表,最后使用 json_encode 函数将其转换为 JSON 格式。这段代码对你有用,试试这个,

$str="102_264,102_273,104_225,103_233,103_229,103_232,101_316,101_244,101_246,101_249";

 $arrayComma = explode(',', $str);

 $menu = array();
 $subMenu = array();

 foreach($arrayComma AS $val){
      list($menu[], $subMenu[]) = explode('_', $val);
 }

 $json = array();

 $json=array("menu" => $menu, "submenu" => $subMenu);

 echo json_encode($json);

编码愉快..!

【讨论】:

    【解决方案2】:

    您正在寻找爆炸功能。

    $array = explode($string,','); //Will contain array $array[0] = "102_264" etc
    
    foreach($array as $arr){
       $explodedArr = explode($arr, '_');
       $menuId = $explodedArr[0]; //Contains 102
       $subMenuId = $explodedArr[1]; //Contains 264
    }
    

    【讨论】:

      【解决方案3】:
      $str='102_264,102_273,104_225,103_233,103_229,103_232,101_316,101_244,101_246,101_249';
      $arr=array();
      $arr=explode(',', $str);
      
      for ($i=0; $i<sizeof($arr) ; $i++) 
      { 
          $arr2=array();
          $arr2=explode('_',$arr[$i]);
          $sql="select * from abc where menu_id=".$arr2[0]." and sub_menu_id=".$arr2[1]."";
          echo $sql."    ";
      }
      

      【讨论】:

        【解决方案4】:

        基本上你想爆炸字符串。 3D数组的快速方法:

        $data = "102_264,102_273,104_225,103_233,103_229,103_232,101_316,101_244,101_246,101_249";
        
        $result = [];
        // retrieve array by splittting string with every comma
        foreach(explode(',', $data) as $value) {
        
            // retrieve Ids by splitting string with underscore
            $serie = explode('_', $value);
            $result[] = array(
                'menuId' => $serie[0],
                'subMenuId' => $serie[1],
            );
        }
        

        您将把所有内容放在单独的子数组中,以便轻松循环查询。

        【讨论】:

          【解决方案5】:

          试试这个,它结合了 explode 和 map 以在一行代码中为您提供数据。

          $results = array_map(function($data){
              return explode("_",$data);
          }, explode(",", $tmp));
          
          foreach($results as $result){
              echo "menuId: " . $result[0]." subMenuId: ".$result[1]."<br />";
          }
          

          给你

          menuId: 102 subMenuId: 264
          menuId: 102 subMenuId: 273
          menuId: 104 subMenuId: 225
          menuId: 103 subMenuId: 233
          menuId: 103 subMenuId: 229
          menuId: 103 subMenuId: 232
          menuId: 101 subMenuId: 316
          menuId: 101 subMenuId: 244
          menuId: 101 subMenuId: 246
          menuId: 101 subMenuId: 249
          

          【讨论】:

            猜你喜欢
            • 2015-03-08
            • 1970-01-01
            • 2022-01-22
            • 2014-05-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多