多维关联数组ID [英] Multidimensional associative array with id
问题描述
我想从3 MySQL表(创建3个维度的数组的部分(ID,标题)=> rubriques (ID,标题)=> 类别(ID,标题):
[0] section_title_1
部分ID 1
[0] rubrique_title_1
rubrique ID 1
[0] category_title_1
类别ID 1
[1] category_title_2
类别ID 2
[2] ........................
[1] rubrique_title_2
rubrique ID 2
...................[1] section_title_2
.........
这是我的code 无 ID:
$输出=阵列();
$部分= $这个 - > _model-> get_table_sections(); 的foreach($部分为$部分){
$输出[$本条> SECTION_TITLE] =阵列(); $ rubriques = $这个 - > _model-> get_rubriques_for_a_section($本条> SECTION_ID);
的foreach($ rubriques为$ rubrique){
$输出[$本条> SECTION_TITLE] [$ rubrique-> rubrique_title] =阵列(); $类= $这个 - > _model-> get_categ_for_a_section_and_a_rubrique($本条> SECTION_ID,$ rubrique-> rubrique_id);
的foreach($类别,$ categorie){
$输出[$本条> SECTION_TITLE] [$ rubrique-> rubrique_title] [] = $ categorie-> categorie_title;
}
}
}
我的问题:
如何让的ID (节,rubrique,在阵列输出类??
我曾尝试:
$输出[$本条> SECTION_TITLE] =阵列(部分编号=> $本条> SECTION_ID); $输出[$本条> SECTION_TITLE] [$ rubrique-> rubrique_title] =阵列(rubrique ID=> $ rubrique-> rubrique_id); $输出[$本条> SECTION_TITLE] [$ rubrique-> rubrique_title] [$ categorie-> categorie_title] =阵列(CATID=> $ categorie-> categorie_id);
但对于类,有问题....
阵列(大小= 41)
'三农'=>
阵列(大小= 4)
部分ID =>串1(长度= 1)
'三农'=>
阵列(大小= 43)
rubrique ID =>串1(长度= 1)
Andaineuse'=>
阵列(大小= 1)
...
Arroseur'=>
阵列(大小= 1)
任何人都可以发现我要去哪里错了吗?帮助是AP preciated!结果
非常感谢............
这可能是答案:
<$p$p><$c$c>$output[$section->section_title][$rubrique->rubrique_title][$categorie->categorie_title] =阵列(CATID=&GT; $ categorie-&GT; categorie_id);不过,我会建议你在数组,而不是标题使用ID为键
<$p$p><$c$c>$output[$section->section_id][$rubrique->rubrique_id][$categorie->categorie_id][] = $ categorie-&GT; categorie_title;这是更加清晰,独特,它应该工作,只要你想。在的foreach你可以得到钥匙(ID)的轻松:
的foreach($输出$关键=&GT; $标题){...
i would like to create a array of 3 dimensions from 3 mysql tables(sections(id,title) => rubriques(id,title) => category(id,title):
[0] section_title_1
section id 1
[0] rubrique_title_1
rubrique id 1
[0] category_title_1
category id 1
[1] category_title_2
category id 2
[2] ........................
[1] rubrique_title_2
rubrique id 2
...................
[1] section_title_2
.........
here is my code WITHOUT id:
$output = array();
$sections = $this->_model->get_table_sections();
foreach($sections as $section){
$output[$section->section_title] = array();
$rubriques = $this->_model->get_rubriques_for_a_section($section->section_id);
foreach($rubriques as $rubrique){
$output[$section->section_title][$rubrique->rubrique_title] = array();
$categories =$this->_model-> get_categ_for_a_section_and_a_rubrique($section->section_id,$rubrique->rubrique_id);
foreach($categories as $categorie){
$output[$section->section_title][$rubrique->rubrique_title][] = $categorie->categorie_title;
}
}
}
My question:
how to have the ID (section,rubrique,category in the array OUTPUT ??
i have tried :
$output[$section->section_title] = array("section id" => $section->section_id);
$output[$section->section_title][$rubrique->rubrique_title] = array("rubrique id" => $rubrique->rubrique_id);
$output[$section->section_title][$rubrique->rubrique_title][$categorie->categorie_title] = array("catid" => $categorie->categorie_id);
but for category , there is problem....
array (size=41)
'Agriculture' =>
array (size=4)
'section id' => string '1' (length=1)
'Agriculture' =>
array (size=43)
'rubrique id' => string '1' (length=1)
'Andaineuse' =>
array (size=1)
...
'Arroseur' =>
array (size=1)
Can anyone spot where I'm going wrong? Help is appreciated!
Many thanks............
This might be the answer:
$output[$section->section_title][$rubrique->rubrique_title][$categorie->categorie_title] = array("catid" => $categorie->categorie_id);
But I would suggested you use IDs as keys in array instead of titles.
$output[$section->section_id][$rubrique->rubrique_id][$categorie->categorie_id][] = $categorie->categorie_title;
It's more clear, unique and it should work as you want. In foreach you can get keys (IDs) easily:
foreach($output as $key => $title) { ...
这篇关于多维关联数组ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!