/    Sign up×
Community /Pin to ProfileBookmark

2 dimensional associative array maybe?

from this query

[code]
$categorysubcategorydescripttitle = $connect->query(“SELECT CategoryTitle, SubCategoryTitle, SubCategoryDescription_Lrg FROM lkuTbl_Category LEFT JOIN lkuTbl_SubCategory ON FK_CategoryId = PK_CategoryId”);
while($catsubcat_descripttitle = $categorysubcategorydescripttitle->fetch_assoc()) {
print_r($catsubcat_descripttitle);
}
[/code]

I get the following in form of an associative array

[code]
Array
(
[CategoryTitle] => Traditional Favourites
[SubCategoryTitle] => Dosai
[SubCategoryDescription_Lrg] => Pancake or crepe made from a fermented batter of rice flour and urid dhal (black gram/ lentil) served with sambar (a lentil based vegetable stew or chowder, cooked with tamarind broth and seasoned with exotic spices) along with two chutneys
)
Array
(
[CategoryTitle] => Traditional Favourites
[SubCategoryTitle] => Steamed Rice Cakes
[SubCategoryDescription_Lrg] => Type of savoury rice cakes made by steaming a fermented batter consisting of rice flour and urid dhal (black gram/ lentil) served with sambar (a lentil based vegetable stew or chowder, cooked with tamarind broth and seasoned with exotic spices) along with two chutneys
)
Array
(
[CategoryTitle] => Traditional Favourites
[SubCategoryTitle] => Crispy Breads
[SubCategoryDescription_Lrg] =>
)
Array
(
[CategoryTitle] => Starters
[SubCategoryTitle] =>
[SubCategoryDescription_Lrg] =>
)
Array
(
[CategoryTitle] => Mains
[SubCategoryTitle] =>
[SubCategoryDescription_Lrg] =>
)
[/code]

but WHAT I’M TRYING TO ACHIEVE IS SOMETHING LIKE BELOW:

[code]
Array
(
[Traditional Favourites] => Array
(
[SubCategoryTitle] => Dosai
[SubCategoryTitle] => Steamed Rice Cakes
[SubCategoryTitle] => Crispy Breads
)
)
[/code]

but what I’ve got is this below:

[code]
Array
(
[Traditional Favourites] => Array
(
[SubCategoryTitle] => Dosai
)
)
Array
(
[Traditional Favourites] => Array
(
[SubCategoryTitle] => Steamed Rice Cakes
)
)
Array
(
[Traditional Favourites] => Array
(
[SubCategoryTitle] => Crispy Breads
)
)
[/code]

Using the below:

[code]
$categorysubcategorydescripttitle = $connect->query(“SELECT CategoryTitle, SubCategoryTitle, SubCategoryDescription_Lrg FROM lkuTbl_Category LEFT JOIN SubCategory ON FK_CategoryId = PK_CategoryId”);
while($catsubcat_descripttitle = $categorysubcategorydescripttitle->fetch_assoc()) {
foreach($catsubcat_descripttitle as $key => $value) {
if($key == ‘SubCategoryTitle’ && ($value != NULL | $value != ”)) {
$subcategory_title[$key] = $value;
}
}

if($catsubcat_descripttitle[‘CategoryTitle’] == “Traditional Favourites”) {
$catsubcat[$catsubcat_descripttitle[‘CategoryTitle’]] = $subcategory_title;
print_r($catsubcat);
}
}
// while($catsubcat_descripttitle = $categorysubcategorydescripttitle->fetch_assoc())
[/code]

can someoe possibly help? please

__(Added some more `[code]…[/code]` tags ~ MOD)__

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 29.2019 — Firstly, you cannot have this, as you cannot have multiple instances of the same array key:
<i>
</i>Array
(
[Traditional Favourites] =&gt; Array
(
[SubCategoryTitle] =&gt; Dosai
[SubCategoryTitle] =&gt; Steamed Rice Cakes
[SubCategoryTitle] =&gt; Crispy Breads
)
)

Each subsequent assignment using the same key will overwrite the previous assignment.

You could instead make something like:
<i>
</i>Array
(
[Traditional Favourites] =&gt; Array
(
[SubCategoryTitles] =&gt; Array
(
0 =&gt; Dosai
1 =&gt; Steamed Rice Cakes
2 =&gt; Crispy Breads
)
)
)

Perhaps something this simple:
<i>
</i>$catsubcat = array();
while($catsubcat_descripttitle = $categorysubcategorydescripttitle-&gt;fetch_assoc()) {
if(!empty($catsubcat_descripttitle['SubCategoryTitle'])) {
$catsubcat[$catsubcat_descripttitle['CategoryTitle']]['SubCategoryTitles'][] =
$catsubcat_descripttitle['SubCategoryTitle'];
}
}
Copy linkTweet thisAlerts:
@nsathauthorOct 29.2019 — Perfect thank you so much. If it's not too much trouble could explain or at least correct me if I'm wrong

The output now is an associative array ([Traditional Favourites]) with the value of an associative aray ([SubCategoryTitles]) with the value for this being an indexed array
Copy linkTweet thisAlerts:
@NogDogOct 29.2019 — @nsath#1610468 Yeah, I think that's what I was visualizing. :) (I did not test it. ;) )
Copy linkTweet thisAlerts:
@nsathauthorOct 29.2019 — I've tested it, It's output is what I was looking for but I'm trying to understand it and also loop through which is what I'm doing/ or trying to do now
Copy linkTweet thisAlerts:
@nsathauthorOct 29.2019 — also the field SubCategoryDescription_Lrg is linked to the SubCategoryTitle field would this another dimension? and would it be possible for you to provide some form of solution
Copy linkTweet thisAlerts:
@NogDogOct 29.2019 — @nsath#1610480

Maybe?...
<i>
</i>while($catsubcat_descripttitle = $categorysubcategorydescripttitle-&gt;fetch_assoc()) {
if(!empty($catsubcat_descripttitle['SubCategoryTitle'])) {
$catsubcat[$catsubcat_descripttitle['CategoryTitle']]['SubCategoryTitles'][] = array(
'title' =&gt; $catsubcat_descripttitle['SubCategoryTitle'],
'description' =&gt; $catsubcat_descripttitle['SubCategoryDescription_Lrg']
);
}
}
Copy linkTweet thisAlerts:
@nsathauthorOct 30.2019 — @NogDog#1610481 Perfect thank you soo much
Copy linkTweet thisAlerts:
@nsathauthorOct 31.2019 — In the same way I'm trying to list the dish titles along with the price

for each vegetarian dish the intention is to display an icon of some sort to indicate this

as you can probably tell I'm not too good at manipulating associative/ multidimensional arrays and I'm not sure as too even approach this

all I've got down so far is:

<i>
</i>$subcatdishtitle_price = $connect-&gt;query("SELECT MenuItemTitle, MenuItemType, MenuItemPrice FROM tblMenuItem RIGHT JOIN SubCategory ON FK_SubCategoryId = PK_SubCategoryId WHERE SubCategoryTitle = '$subcattitle' ");
while($subcat_dishtitleprice = $subcatdishtitle_price-&gt;fetch_assoc()) {

}
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.11,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...