我想在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
)
102
是menuId
264
是subMenuId
所以,我想在上面的表中将此String与fire查询分开。 我怎么能这样做?
答案 0 :(得分:2)
您可以使用爆炸功能或使用列表,最后使用 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);
快乐的编码..!
答案 1 :(得分:0)
您正在寻找爆炸功能。
$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
}
答案 2 :(得分:0)
$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." ";
}
答案 3 :(得分:0)
基本上你想爆炸字符串。快速的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],
);
}
您将拥有单独的子阵列中的所有内容,以便轻松循环查询。
答案 4 :(得分:0)
试试这个,它结合了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