概述
$rs = Db::name('register')
->alias('r')
->field('r.id,r.user_id')
->join('register_class c','r.class_id = c.id')//模块
->where('r.deleted',0)
->where('c.deleted',0)
->select();
if(count($rs)>0){
//
foreach($rs as $key => $v1){
//
$useridarr[]=$v1['user_id'];
//
}
$useridarr = array_column($rs,'user_id');
另一种办法:array[1,2,45]
foreach for循环组装成一维数组
$useridarr[]=$v['user_id'];
另一种办法:array[{id:1,titl:标题1},{id:2,titl:标题2}]
foreach($cidarr as $cid){
$rs4=Db::name('xx')->field('id,title')->where('deleted',0)->select();
if(count($rs4)>0){
foreach($rs4 as $k=> $v4){
$uidarr[] = $v4['id'];
$arr['id'] = $v4['id'];
$arr['title'] = $v4['title'];
$uidtitlearr[] = $arr;
}
}
}
$uidarr输出:array[1,2,45]
$uidtitlearr输出:array[{id:1,titl:标题1},{id:2,titl:标题2}]
教程:https://blog.csdn.net/haibo0668/article/details/87890325
最终输出json数据
数据源格式
拼装数组代码:
<meta charset="utf-8">
<?php
//"北京理工大学": {"val": "110000","items": {"高起升": {"val": "210000","tuition": "80000","items": {"人力资源管理": "1","工商企业管理": "2"}},"专升本": {"val": "220000","tuition": "2000","items": {"人力": "3","工商": "4"}}}},
//{"南开大学": {"val": "675", "items": {"专升本": {"val": "683","tuition": "7500","items": {"报考专业2": "685","报考专业1": "684"}},"高起专": {"val": "682","tuition": "6520","items": {"报考专业4": "687","报考专业3": "686"}}},"北京理工大学": {"val": "674", "items": {"专升本": {"val": "677","tuition": "8600","items": {"人力资源管理2": "679","人力资源管理": "678"}},"高起专": {"val": "676","tuition": "3200","items": {"工商企业管理": "681","工商企业管理1": "680"}}}}
//样板数组
$arr0 = array(
"北京理工大学303" => $arr1 = array(
"val" => "110000",
"items" => array(
"高起升" => $arr2 = array(
"val" => "210000",
"tuition" => "80000",
"items" => $arr3 = array(
"人力资源管理" => "1",
"工商企业管理" => "2"
)
),
"专升本" => $arr2 = array(
"val" => "210000",
"tuition" => "80000",
"items" => $arr3 = array(
"人力资源管理" => "1",
"工商企业管理" => "2"
)
)
)
)
);
//随便给一些,然后数拼装数组,输出上面一样子的数据格式
$st="303,304";
$rs = explode(',',$st);
for($i=0;$i<count($rs);$i++){
//拼装一级数组名称
$array1="array1_".$rs[$i];//一级数组名称:array1_303
array1_304
//2级
//拼装二级数组名称
$array2="array2_".$rs[$i];// 名称 二级数组名称:array2_303
array2_304
if($rs[$i]==303){
$st2="32,36";//层次 id
}elseif($rs[$i]==304){
$st2="12,5";//层次 id
}
$rs2 = explode(',',$st2);
for($k=0;$k<count($rs2);$k++){
//3级
//拼装3级数组名称
$array3="array3_".$rs[$i]."_".$rs2[$k];// 名称 3级数组名称:array2_303_12
array2_304_125
if($rs2[$k]==32){
${$array3}['人力资源管理'.$rs2[$k]] = $k;
${$array3}['工商企业管理'.$rs2[$k]] = 2;
}elseif($rs2[$k]==36){
${$array3}['人力'.$rs2[$k]] = 1;
${$array3}['工商'.$rs2[$k]] = 2;
}elseif($rs2[$k]==12){
${$array3}['人力'.$rs2[$k]] = 1;
${$array3}['工商'.$rs2[$k]] = 2;
}elseif($rs2[$k]==5){
${$array3}['人力'.$rs2[$k]] = 1;
${$array3}['工商'.$rs2[$k]] = 2;
}
//拼装二级数组名称
$array21="array21_".$rs[$i]."_".$rs2[$k];// 内容 二级数组名称:array2_303_12
array2_304_125
//2级 内容ID
${$array21}['val'] = "210".$rs[$i].$rs2[$k];
${$array21}['tuition'] = "800".$rs[$i].$rs2[$k];
${$array21}['items'] = $$array3;
//2级 名称
//${$array2}[$rs[$i].'高起升'] = $$array21;
${$array2}['专升本_'.$rs[$i]."_".$rs2[$k]] = $$array21;
}
//1级
${$array1}['val'] = 1000+$rs[$i];
${$array1}['items'] = $$array2;
$array[$rs[$i]] = $$array1;
}
echo "<pre>";
print_r($arr0);
echo "<hr>";
print_r($array);
echo "</pre>";
//echo json_encode($arr1, JSON_UNESCAPED_UNICODE); //显示中文
?>
实例:用TP5操作数据库
//学校 层次 价格 专业 读出接口数据
public function addstudenttable()
{
//
$arr = array(
//
//
"北京理工大学" =>array(
//
"val" => "110000",
//
"items" => array(
//
"高起升" => array(
//
"val" => "210000",
//
"tuition" => "80000",
//
"items" => array(
//
"人力资源管理" => "1",
//
"工商企业管理" => "2"
//
)
//
),
//
"专升本" => array(
//
"val" => "21002",
//
"tuition" => "54000",
//
"items" => array(
//
"人力资源管理" => "1",
//
"工商企业管理" => "2"
//
)
//
)
//
)
//
),
//
);
//
//
print_r (json_encode($arr, JSON_UNESCAPED_UNICODE));
$rs=Db::name('school')->where('flid',1)->select();//学校
$count = count($rs);
if($count){//1级 学校
foreach($rs as $schoolrs){
//拼装一级数组名称
$array1="array1_".$schoolrs['id'];//一级数组名称:array1_303
array1_304
//2级
//拼装二级数组名称
$array2="array2_".$schoolrs['id'];// 名称 二级数组名称:array2_303
array2_304
$rs2=Db::name('school')->where('pid',$schoolrs['id'])->select();
$count2 = count($rs2);
if($count2){
foreach($rs2 as $schoolrs2){
//3级
//拼装3级数组名称
$array3="array3_".$schoolrs['id']."_".$schoolrs2['id'];// 名称 3级数组名称:array2_303_12
array2_304_125
//3级 专业
$rs3=Db::name('school')->where('pid',$schoolrs2['id'])->select();
$count3 = count($rs3);
if($count3){
foreach($rs3 as $schoolrs3){
${$array3}[$schoolrs3['title']] = $schoolrs3['id'];
}
}
//拼装二级数组名称
$array21="array21_".$schoolrs['id'].$schoolrs2['id'];// 内容 二级数组名称:array2_303_12
array2_304_125
//2级 内容ID
${$array21}['val'] = $schoolrs2['id'];
${$array21}['tuition'] = $schoolrs2['price'];
${$array21}['items'] = $$array3;
//2级 名称
//${$array2}[$rs[$i].'高起升'] = $$array21;
${$array2}[$schoolrs2['title']] = $$array21;
}
}
//1级
${$array1}['val'] = $schoolrs['id'];
${$array1}['items'] = $$array2;
$array[$schoolrs['title']] = $$array1;
}
}
print_r (json_encode($array, JSON_UNESCAPED_UNICODE)); //显示中文
}
其它学习点:
输出字符串:
{"南开大学": {"val": "675", "items": {"专升本": {"val": "683","tuition": "7500","items": {"报考专业2": "685","报考专业1": "684"}},"高起专": {"val": "682","tuition": "6520","items": {"报考专业4": "687","报考专业3": "686"}}},"北京理工大学": {"val": "674", "items": {"专升本": {"val": "677","tuition": "8600","items": {"人力资源管理2": "679","人力资源管理": "678"}},"高起专": {"val": "676","tuition": "3200","items": {"工商企业管理": "681","工商企业管理1": "680"}}}}
输出字符串实例代码:
//
$rs=Db::name('school')->where('flid',1)->select();//学校
//
$count = count($rs);
//
if($count){//1级 学校
//
foreach($rs as $schoolrs){
//
//2级 层次
//
$rs2=Db::name('school')->where('pid',$schoolrs['id'])->select();
//
$count2 = count($rs2);
//
$allrs2 ="";
//
if($count2){
//
foreach($rs2 as $schoolrs2){
//
//3级 专业
//
$rs3=Db::name('school')->where('pid',$schoolrs2['id'])->select();
//
$count3 = count($rs3);
//
$allrs3 ="";
//
if($count3){
//
foreach($rs3 as $schoolrs3){
//
$allrs3='"'.$schoolrs3['title'].'": "'.$schoolrs3['id'].'"'.",".$allrs3;
//
//$allrs3=$schoolrs3['title'].": ".$schoolrs3['id'].",".$allrs3;
//
//$allrs3 .=$schoolrs3['title'].": ".$schoolrs3['id'].",";
//
}
//
}
//
$allrs3 = substr($allrs3,0,strlen($allrs3)-1); //去掉最后一个字符,
//
//
$allrs2='"'.$schoolrs2['title'].'": {"val": "'.$schoolrs2['id'].'","tuition": "'.$schoolrs2['price'].'","items": {'.$allrs3.'}}'.",".$allrs2;
//
//$allrs2=$schoolrs2['title'].': {"val": '.$schoolrs2['id'].',"tuition": '.$schoolrs2['price'].',"items": {'.$allrs3.'}}'.",".$allrs2;
//
}
//
$allrs2 = substr($allrs2,0,strlen($allrs2)-1); //去掉最后一个字符,
//
}
//
//
//"北京理工大学": {"val": "110000","items": {"高起升": {"val": "210000","tuition": "80000","items": {"人力资源管理": "1","工商企业管理": "2"}},"专升本": {"val": "220000","tuition": "2000","items": {"人力": "3","工商": "4"}}}},
//
//$allrs1='"'.$schoolrs['title'].'": {"val": "'.$schoolrs['id'].'","items": {"高起升": {"val": "210000","tuition": "80000","items": {"人力资源管理": "1","工商企业管理": "2"}},"专升本": {"val": "220000","tuition": "2000","items": {"人力": "3","工商": "4"}}}},';
//
$allrs1='"'.$schoolrs['title'].'": {"val": "'.$schoolrs['id'].'", "items": {'.$allrs2.'}'.",".$allrs1;
//
//$allrs1=$schoolrs['title'].': {"val": '.$schoolrs['id'].', "items": {'.$allrs2.'}'.",".$allrs1;
//
//$allrs=$allrs.","."11";
//
//
}
//
$allrs1 = substr($allrs1,0,strlen($allrs1)-1); //去掉最后一个字符,
//
$allrs ='{'.$allrs1.'}';
//
echo $allrs;
//
echo (json_decode($allrs));
//
//echo json_encode($allrs, JSON_UNESCAPED_UNICODE);
//
}
最后
以上就是称心柚子为你收集整理的tp5.1 php 拼装/组装一维 二维 多维数组 foreach循环$useridarr[]=$v['user_id'];另一种办法:array[{id:1,titl:标题1},{id:2,titl:标题2}]的全部内容,希望文章能够帮你解决tp5.1 php 拼装/组装一维 二维 多维数组 foreach循环$useridarr[]=$v['user_id'];另一种办法:array[{id:1,titl:标题1},{id:2,titl:标题2}]所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复