/    Sign up×
Community /Pin to ProfileBookmark

how do i change the first element to last associative array

This is the result of a query in the form of associative array

[code]
Array
(
[Biriyani] => Array
(
[Long-grained rice flavored with exotic spices for example saffron and coriander mixed with vegetables, egg and/or optionally with any meat or seafood] => Array
(
[Priya Special Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 7.99
)
)
)
[Vegetable Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Vegetarian
[dish_price] => 4.45
)
)
)
[Egg Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 4.95
)
)
)
[Chicken Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 5.95
)
)
)
[Mutton Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 6.25
)
)
)
[Prawn Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 6.50
)
)
)
[Seafood Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 7.25
)
)
)
[Hyderabadi Chicken Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 6.49
)
)
)
[Hyderabadi Mutton Biriyani] => Array
(
[] => Array
(
[0] => Array
(
[dish_type] => Non-Vegetarian
[dish_price] => 6.99
)
)
)
)
)
)
[/code]

However from the database the Priya Special Biriyani is the last item but some reason shows up as the first.
How would I go about rearranging it. Its just the first should be last I’ve tried array_push() but it shows as the first and the last

Any Ideas, Solution much appreciated

to post a comment
PHP

29 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 15.2019 — Ideally via the database query if there is a sort clause that would do the trick. (The "order" that rows appear in the database query is effectively undefined if you don't specify one or more result columns on which to sort.)
Copy linkTweet thisAlerts:
@ginerjmNov 15.2019 — Are you only concerned about the first item in your results or ALL of the items being in a certain order?
Copy linkTweet thisAlerts:
@nsathauthorNov 16.2019 — @ginerjm#1610907 yeh its just the first item which should be last, the remaining should be in natural order in other words how it stand now
Copy linkTweet thisAlerts:
@ginerjmNov 16.2019 — So you like the order of everything except one item.

One thing you could do is to create a field in your select statement that is created as the result of a case operand (look it up) that assigns a sort value to it. Then you could assign a 0 to that field for that single particular record and a 1 to all the other records and use that field along with any other sequencing fields you prefer in the order by clause.
Copy linkTweet thisAlerts:
@nsathauthorNov 16.2019 — @ginerjm#1610925 any idea on roughly what that would look like? Its just I've personally never come across fields
Copy linkTweet thisAlerts:
@NogDogNov 16.2019 — I think he's suggesting something like this?
<i>
</i>$sql = "
SELECT
col_1,
col_2,
col_3,
CASE WHEN col_3 = 'some specific value' THEN 0 ELSE 1 END AS sort_order,
col_4
-- FROM and WHERE clauses here, then
ORDER BY sort_order
";
Copy linkTweet thisAlerts:
@ginerjmNov 17.2019 — eggzackly!
Copy linkTweet thisAlerts:
@VITSUSANov 18.2019 — @ginerjm#1610947 What is this?
Copy linkTweet thisAlerts:
@nsathauthorNov 19.2019 — @NogDog#1610939 I understand the logic behind but putting I can it half and half as in the special dishes comes last as expected/ wished for so the CASE WHEN col_3 = 'some specific value' THEN 0 ELSE which in my case is ORDER BY CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 but the natural ordering part for the remaining does not seem to work any ideas?
Copy linkTweet thisAlerts:
@cootheadNov 19.2019 — > @VITSUSA#1610954 [ginerjm](https://www.webdeveloper.com/d/386774/8) What is this?

It's just his personal way of spelling **exactly**. 😄

_coothead_
Copy linkTweet thisAlerts:
@ginerjmNov 19.2019 — you moved the example of using the CASE into the order by clause. No. It is part of the select process and belongs there instead.
Copy linkTweet thisAlerts:
@nsathauthorNov 19.2019 — @ginerjm#1610984 ok yep your right its just before @NogDog replied I had found something similar via stackoverflow which declared the case in the order by clause.
Copy linkTweet thisAlerts:
@NogDogNov 19.2019 — @nsath#1610991 Depending on the field type, you might need an ELSE to set the search value for other values, _e.g.:_ ORDER BY CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 2. (I've never used a case in an order by clause, so no guarantees.)
Copy linkTweet thisAlerts:
@ginerjmNov 20.2019 — OP - so have you solved your problem? Or do you want to show us your query statement for review now that you have fixed it?
Copy linkTweet thisAlerts:
@nsathauthorNov 21.2019 — @ginerjm#1611025 I haven't managed to solve this as of yet possibly a review or ideas would be ideal below is this full query

<i>
</i>SELECT MenuItemTitle, CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END AS cust_sort, MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = Category_PK_CategoryId LEFT JOIN lkuTbl_PriyaSpecial ON FK_SpecCategoryId = PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = PK_SubCategoryId WHERE Category_CategoryTitle = '$categorysubcattitle'
OR SubCategory_SubCategoryTitle = '$categorysubcattitle' ORDER BY cust_sort
Copy linkTweet thisAlerts:
@ginerjmNov 21.2019 — So - what is wrong now?
Copy linkTweet thisAlerts:
@NogDogNov 21.2019 — 1 is larger than 0, so a default ascending sort will have the 1 come after all the zeros. I suspect you want ...THEN 0 ELSE 1...?
Copy linkTweet thisAlerts:
@nsathauthorNov 21.2019 — @ginerjm#1611057 the SQL query throws out an error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END AS cust_sort, Me' at line 1
Copy linkTweet thisAlerts:
@nsathauthorNov 21.2019 — @ginerjm#1611057 also if I use the query:

<i>
</i>SELECT MenuItemTitle, CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END, MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = Category_PK_CategoryId LEFT JOIN lkuTbl_PriyaSpecial ON FK_SpecCategoryId = PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = PK_SubCategoryId WHERE Category_CategoryTitle = '$categorysubcattitle'
OR SubCategory_SubCategoryTitle = '$categorysubcattitle'

without the AS

the special dish comes last as wished for but the remaining is not in natural order
Copy linkTweet thisAlerts:
@nsathauthorNov 21.2019 — @NogDog#1611064 I wish to display the special dish last and I get this with CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END so that's fine but remaining normal dishes I want to be able to display them in natural/ default order in order words, the order in which it is in the database
Copy linkTweet thisAlerts:
@NogDogNov 21.2019 — @nsath#1611069

May need to do ...ORDER BY cost_sort, MenuItemTitle
Copy linkTweet thisAlerts:
@nsathauthorNov 22.2019 — @NogDog#1611070 nah that doesn't work it displays everything without any ordering at all in other how it was from the beginning
Copy linkTweet thisAlerts:
@ginerjmNov 22.2019 — Can we see the query statement that you tried?
Copy linkTweet thisAlerts:
@nsathauthorNov 22.2019 — @ginerjm#1611093

I've tried both the following with the same result:

<i>
</i>SELECT MenuItemTitle, CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END, MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = Category_PK_CategoryId LEFT JOIN lkuTbl_PriyaSpecial ON FK_SpecCategoryId = PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = PK_SubCategoryId WHERE Category_CategoryTitle = '$categorysubcattitle'
OR SubCategory_SubCategoryTitle = '$categorysubcattitle'


and:

<i>
</i>SELECT MenuItemTitle, CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END AS cust_sort, MenuItemDescription_LrgDesc, MenuItemType, MenuItemPrice, lkuTbl_Category_CategoryTitle, lkuTbl_SpecCategory_Title, lkuTbl_SubCategory_SubCategoryTitle, lkuTbl_SubCategory_SubCategoryDescription_LrgDesc FROM tblMenuItem LEFT JOIN lkuTbl_Category ON FK_CategoryId = lkuTbl_Category_PK_CategoryId LEFT JOIN lkuTbl_PriyaSpecial ON FK_SpecCategoryId = lkuTbl_SpecCategory_PK_SpecCategoryId LEFT JOIN lkuTbl_SubCategory ON FK_SubCategoryId = lkuTbl_SubCategory_PK_SubCategoryId WHERE lkuTbl_Category_CategoryTitle = '$categorysubcattitle' OR lkuTbl_SubCategory_SubCategoryTitle = '$categorysubcattitle'ORDER BY cust_sort


Both run without any errors. The special dish come after all the others fine but the other dishes are not in natural order
Copy linkTweet thisAlerts:
@ginerjmNov 23.2019 — Here is my cleaned-up version of your queries in true php-code format:
<i>
</i> // version 1
$q1 = "SELECT MenuItemTitle,
CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END,
MenuItemDescription, MenuItemType, MenuItemPrice, Category_CategoryTitle, SpecCategory_Title, SubCategory_SubCategoryTitle, SubCategoryDescription
FROM tblMenuItem

<i> </i> LEFT JOIN lkuTbl_Category
<i> </i> ON FK_CategoryId = Category_PK_CategoryId
<i> </i>
<i> </i> LEFT JOIN lkuTbl_PriyaSpecial
<i> </i> ON FK_SpecCategoryId = PK_SpecCategoryId

<i> </i> LEFT JOIN lkuTbl_SubCategory
<i> </i> ON FK_SubCategoryId = PK_SubCategoryId
<i> </i>
<i> </i> WHERE Category_CategoryTitle = '$categorysubcattitle' OR
<i> </i> SubCategory_SubCategoryTitle = '$categorysubcattitle'";
<i> </i>
<i> </i>// version 2
<i> </i>$q2 = "SELECT MenuItemTitle,
<i> </i> CASE WHEN MenuItemTitle LIKE 'Priya Special%' THEN 1 ELSE 0 END AS cust_sort,
<i> </i> MenuItemDescription_LrgDesc, MenuItemType, MenuItemPrice, lkuTbl_Category_CategoryTitle, lkuTbl_SpecCategory_Title, lkuTbl_SubCategory_SubCategoryTitle, lkuTbl_SubCategory_SubCategoryDescription_LrgDesc
<i> </i>
<i> </i> FROM tblMenuItem
<i> </i>
<i> </i> LEFT JOIN lkuTbl_Category
<i> </i> ON FK_CategoryId = lkuTbl_Category_PK_CategoryId
<i> </i> LEFT JOIN lkuTbl_PriyaSpecial
<i> </i> ON FK_SpecCategoryId = lkuTbl_SpecCategory_PK_SpecCategoryId
<i> </i>
<i> </i> LEFT JOIN lkuTbl_SubCategory
<i> </i> ON FK_SubCategoryId = lkuTbl_SubCategory_PK_SubCategoryId
<i> </i>
<i> </i> WHERE lkuTbl_Category_CategoryTitle = '$categorysubcattitle' OR
<i> </i> lkuTbl_SubCategory_SubCategoryTitle = '$categorysubcattitle'
<i> </i>
<i> </i> ORDER BY cust_sort";


Version 1 has no sorting involved, hence who knows how the results will come out?

Version 2 has a sort based solely on the results of your case statement. That explains why one record shows up where you want it and the others are random. As you have been instructed previously, add another column to the order by clause to achieve your desired sort sequence!

PS - Note how I have posted your queries in a much easier to read format.
Copy linkTweet thisAlerts:
@nsathauthorNov 23.2019 — @ginerjm#1611121 Note taken, I don't understand what you mean by add another column to the order by clause could you possible provide an example
Copy linkTweet thisAlerts:
@ginerjmNov 23.2019 — "order by cust_sort, anotherfieldname "

cust_sort will get that special guy up top where you want him and anotherfieldname will sort the remaining records with the same value of cust_sort in order behind that guy.

This would be all so easy if you did a bit of reading....
Copy linkTweet thisAlerts:
@nsathauthorDec 17.2019 — sorry to bother gain, after a shit load of reading, I'm still stuck with, could someone please just give me solution, after all the reading I've got a query of
<i>
</i>SELECT
MenuItemTitle AS 'title',
MenuItemDescription_LrgDesc,
MenuItemType,
MenuItemPrice,
lkuTbl_Category_CategoryTitle,
lkuTbl_SpecCategory_Title,
lkuTbl_SubCategory_SubCategoryTitle,
CASE WHEN
lkuTbl_SubCategory_SubCategoryTitle = 'Fried Rice'
OR lkuTbl_SubCategory_SubCategoryTitle = 'Noodles'
OR lkuTbl_SubCategory_SubCategoryTitle = 'Biriyani'
OR lkuTbl_SubCategory_SubCategoryTitle = 'Kothu Roti'
OR lkuTbl_SubCategory_SubCategoryTitle = 'Idiappam (String Hoppers)'
OR lkuTbl_SubCategory_SubCategoryTitle = 'Pittu'
THEN
CASE WHEN
MenuItemTitle LIKE 'Priya Special%'
THEN
0 ELSE 1
END
END AS 'dish_sort',
lkuTbl_SubCategory_SubCategoryDescription_LrgDesc
FROM tblMenuItem
LEFT JOIN lkuTbl_Category
ON FK_CategoryId = lkuTbl_Category_PK_CategoryId
LEFT JOIN lkuTbl_PriyaSpecial
ON FK_SpecCategoryId = lkuTbl_SpecCategory_PK_SpecCategoryId
LEFT JOIN lkuTbl_SubCategory
ON FK_SubCategoryId = lkuTbl_SubCategory_PK_SubCategoryId
WHERE lkuTbl_Category_CategoryTitle = '$categorysubcattitle'
OR lkuTbl_SubCategory_SubCategoryTitle = '$categorysubcattitle'
ORDER BY 'title, dish_sort'


but now it throws error:

PHP Notice: Undefined index: MenuItemTitle in /home/mnzsm9leak30/public_html/test11.1.php on line 424
Copy linkTweet thisAlerts:
@nsathauthorDec 17.2019 — Never mind, I've figured it out
×

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.12,
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,
)...