Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using this structure to make album lists. I'm using a key so I correspond lists to albums but I don't know how to reverse the output so the items start at the top with lowest key value. Any ideas?

  <?php
 $album = get_post_meta($post->ID, "album", false);
 $album_list = get_post_meta($post->ID, "album_list", false); 
 foreach ($album_list as $album_list) {
     list($key, $value) = explode(',',$album_list);
     $indexed_album_list[$key] = $value;
 }
 foreach ( $album as $each_album) {
     list($key,$album) = explode(',',$each_album);
     echo '<div>'.$album.'</div>';
     echo '<div>';
     if ($album_list = $indexed_album_list[$key]) {
         echo $album_list;
     }  
     else {
         echo '&nbsp';
     }
     echo '</div>';
 }
?>    
share|improve this question
    
What is stored in the album and album_list post meta? –  m0r7if3r Feb 10 '12 at 18:50
    
This is a PHP question, not a WordPress question. –  Chip Bennett Feb 10 '12 at 18:54
    
I use a key like "001,album" and "002,album" with "001,album_list", "002,album_list". I know it's php but wordpress is php and I'm making this widget with wordpress. –  Pollux Khafra Feb 10 '12 at 19:22
    
"I know it's php but wordpress is php and I'm making this widget with wordpress" - that doesn't make this question in-scope. You're talking about how to manipulate arrays, which is a PHP function. It doesn't matter that the array was returned by WordPress. Now, if you re-worded the question, to ask, e.g. how to query posts that have certain post meta keys, and sort by meta key and/or value, then that very much would be in-scope for WPSE. –  Chip Bennett Feb 10 '12 at 19:34
    
Touche =) ...For what it's worth I asked the question on Stackoverflow in both the chat and with a post and didn't get an answer. –  Pollux Khafra Feb 10 '12 at 19:36
show 3 more comments

1 Answer 1

up vote 0 down vote accepted

how about array_reverse()

http://php.net/manual/en/function.array-reverse.php

there are a bunch of array manipulation functions at php.net. i'm always there looking something up. also sort() might be useful

EDIT based on your comments:

sort($album);
print_r($album);

should return:

Array
(
    [0] => 001,Get Rich Or Die Tryin
    [1] => 002,Curtis
    [2] => 003,The Massacre
    [3] => 004,Before I Self Destruct
)
share|improve this answer
    
Right now the order shows the album that I entered from first to last and doesn't go by the keys. I think array_reverse would just change the order by which I entered in the albums. Am I wrong? –  Pollux Khafra Feb 10 '12 at 19:59
    
what does your array look like? can you print_r() it? and try some of the sort() functions. –  helgatheviking Feb 10 '12 at 20:05
    
We need to see your returned data, in order to give you a more precise answer. –  Chip Bennett Feb 10 '12 at 20:05
    
Where do I add print_r() to do that? Every example I find on how to use sort doesnt look anything like the structure I have. I'm really just trying to make it say "asc by order of $key" –  Pollux Khafra Feb 10 '12 at 20:46
    
$album = get_post_meta($post->ID, "album", false); print_r($album); $album_list = get_post_meta($post->ID, "album_list", false); print_r($album_list); really hard to help you more w/o seeing how your data is set up –  helgatheviking Feb 10 '12 at 21:15
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.