niclipse Posted January 21, 2009 Share Posted January 21, 2009 I come from a classic ASP 3.0 background, but have now been filling in for our web developer at my company since his lay off. My question is how do I get this array to display in reverse order so the last item renders as the first? $tabs = array(); $tabs[0]["tabtext"] = "Webcasts"; $tabs[0]["viewall_text"] = "More Webcasts..."; $tabs[0]["viewall_url"] = "$com_domain/events"; $tabs[0]["viewall_target"] = "_self"; $tabs[0]["viewall_title"] = "More Webcasts..."; $tabs[1]["tabtext"] = "Events"; $tabs[1]["viewall_text"] = "More Events..."; $tabs[1]["viewall_url"] = "$com_domain/events"; $tabs[1]["viewall_target"] = "_self"; $tabs[1]["viewall_title"] = "More Events..."; $tabs[2]["tabtext"] = "Training"; $tabs[2]["viewall_text"] = "More Classes..."; $tabs[2]["viewall_url"] = "$com_domain/services/training"; $tabs[2]["viewall_target"] = "_self"; $tabs[2]["viewall_title"] = "More Classes..."; Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/ Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 krsort it Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742490 Share on other sites More sharing options...
niclipse Posted January 21, 2009 Author Share Posted January 21, 2009 Unfortunately, I would have to visually see a working sample (using what I provided) to understand that example. Sorry in advance for my incompetence. Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742491 Share on other sites More sharing options...
printf Posted January 21, 2009 Share Posted January 21, 2009 $tabs = array_reverse( $tabs ); to preserve keys, $tabs = array_reverse( $tabs, true ); Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742492 Share on other sites More sharing options...
niclipse Posted January 21, 2009 Author Share Posted January 21, 2009 $tabs = array_reverse( $tabs ); to preserve keys, $tabs = array_reverse( $tabs, true ); Neither of these suggestions reversed the order at all for me. Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742498 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 $tabsReversed = $tabs; krsort($tabsReversed); var_dump($tabsReversed); $tabsReversed = array_reverse( $tabs, true ); var_dump($tabsReversed); Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742500 Share on other sites More sharing options...
niclipse Posted January 21, 2009 Author Share Posted January 21, 2009 Actually, $tabs is a bad example, my web developer hard corded the output rather than let tabs get dynamically generated. A better example would be $webcast, please help me with the below example: $webcast = array(); $webcast[1]["title"] = "PHP Freaks Help Niclipse"; $webcast[1]["day"] = "28"; $webcast[1]["month"] = "jan"; $webcast[1]["url"] = "http://www.yahoo.com/index.html"; $webcast[1]["linktext"] = "Register Now"; $webcast[1]["target"] = "_blank"; $webcast[1]["anchor"] = "phpfreaks"; $webcast[2]["title"] = "PHP Freaks Help Me Again"; $webcast[2]["day"] = "04"; $webcast[2]["month"] = "nov"; $webcast[2]["url"] = "http://www.google.com/index.php"; $webcast[2]["linktext"] = "Register"; $webcast[2]["target"] = "_blank"; $webcast[2]["anchor"] = "phphelp"; and this is what I've tried to do, but it isn't working: $webcastReversed = $webcast; krsort($webcastReversed); var_dump($webcastReversed); $webcastReversed = array_reverse( $webcast, true ); var_dump($webcastReversed); $webcast[1]["title"] = "PHP Freaks Help Niclipse"; $webcast[1]["day"] = "28"; $webcast[1]["month"] = "jan"; $webcast[1]["url"] = "http://www.yahoo.com/index.html"; $webcast[1]["linktext"] = "Register Now"; $webcast[1]["target"] = "_blank"; $webcast[1]["anchor"] = "phpfreaks"; $webcast[2]["title"] = "PHP Freaks Help Me Again"; $webcast[2]["day"] = "04"; $webcast[2]["month"] = "nov"; $webcast[2]["url"] = "http://www.google.com/index.php"; $webcast[2]["linktext"] = "Register"; $webcast[2]["target"] = "_blank"; $webcast[2]["anchor"] = "phphelp"; for($webcastdata = 1; $webcastdata <= sizeof($webcast); ++$webcastdata) { if($even) { $banding = "_even"; $mouseovering = "onMouseOver=\"style.backgroundColor='#e3e3e3';\" onmouseout=\"style.backgroundColor='#ededed'\""; $even = false; } else { $banding = "_odd"; $mouseovering = "onMouseOver=\"style.backgroundColor='#e3e3e3';\" onmouseout=\"style.backgroundColor='#f9f9f9'\""; $even = true; } if($webcast[$webcastdata]["target"] == "_blank") { $tableUnderloader = "onclick=\"javascript:popUpPage('" . $webcast[$webcastdata]["url"] . "');location.href='$com_domain/events/#" . $webcast[$webcastdata]["anchor"] . "'\""; } else { $tableUnderloader = "onclick=\"location.href='" . $webcast[$webcastdata]["url"] . "#" . $webcast[$webcastdata]["anchor"] . "'\""; } $calImage_top = "<img src=\"$com_domain/images/home_dtb/calendar/cal_" . $webcast[$webcastdata]["month"] . ".png\" width=\"33\" height=\"16\" border=\"0\">"; $calImage_bottom = "<img src=\"$com_domain/images/home_dtb/calendar/cal_" . $webcast[$webcastdata]["day"] . ".png\" width=\"33\" height=\"19\" border=\"0\">"; $calIconTable = "<div style=\"text-align: center; padding: 5px 0px 5px 0px;\">"; $calIconTable .= "$calImage_top<br />$calImage_bottom"; $calIconTable .= "</div>"; $webCastContent .= "<table class=\"caltable$banding\" border=\"0\" $mouseovering $tableUnderloader cellpadding=\"0\" cellspacing=\"0\" width=\"242\" height=\"48\" title=\"" . $webcast[$webcastdata]["title"] . "\">"; $webCastContent .= "<tr><td width=\"45\">$calIconTable"; $webCastContent .= "</td><td width=\"197\">"; $webCastContent .= $webcast[$webcastdata]["title"] . "<br><a href=\"#\" title=\"" . $webcast[$webcastdata]["linktext"] . "\">" . $webcast[$webcastdata]["linktext"] . "</a>"; $webCastContent .= "</td></tr></table>"; } Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742518 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 Basically you would change the direction of for loop for($webcastdata = sizeof($webcast); $webcastdata >= 1; --$webcastdata) { Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742523 Share on other sites More sharing options...
niclipse Posted January 21, 2009 Author Share Posted January 21, 2009 Basically you would change the direction of for loop for($webcastdata = sizeof($webcast); $webcastdata >= 1; --$webcastdata) { BINGO!! Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742527 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 For your future reference. foreach is much nicer way of iterating through an array. Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742536 Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Share Posted January 21, 2009 For your future reference. foreach is much nicer way of iterating through an array. but can you do it in reverse? Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742567 Share on other sites More sharing options...
Mchl Posted January 21, 2009 Share Posted January 21, 2009 Once the keys are reversed $arr = array("a","b","c"); foreach ($arr as $key => $value) { echo "$key: $value \n</br>"; } krsort($arr); foreach ($arr as $key => $value) { echo "$key: $value \n</br>"; } Link to comment https://forums.phpfreaks.com/topic/141817-question-make-array-to-display-in-reverse-order/#findComment-742583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.