SoLoGHoST
SoLoGHoST

Reputation: 2689

PHP - Order info from 2 arrays

Ok, I have 2 arrays. 1 array has the ORDER of the information that should be displayed, and the other array has the information. But I am having trouble trying to determine a way to display the information in the order it is supposed to be displayed in...

For example, I have this array here, OF WHICH I HAVE NO CONTROL OVER (meaning the array gets done prior to my code even running, so I have to START with this array of which I can NOT alter this actual array. I could build an array from this array if it would help any? But not sure how...?)

$attach_info(0,1,5,4);

The numerical values here point to actual information to be displayed...

The other array gets populated within a function and get RETURNED from that function with all of the information to be displayed...

The returned information is this array:

while ($row = $smcFunc['db_fetch_assoc']($request))
{
$attachments[$row['id_attach']] = array(
    'member' => array(
    'id' => $row['id_member'],
    'name' => $row['poster_name'],
    'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
    ),
    'file' => array(
    'filename' => $filename,
    'filesize' => round($row['filesize'] /1024, 2) . $txt['kilobyte'],
    'downloads' => $row['downloads'],
    'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
    'link' => '<img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /> <a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '">' . $filename . '</a>',
    'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
    'image' = array(
        'id' => $id_thumb,
        'width' => $row['width'],
        'height' => $row['height'],
        'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" />',
        'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" />',
        'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
        'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image"><img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" /></a>',
        ),
    ),
    'topic' => array(
    'id' => $row['id_topic'],
    'subject' => $row['subject'],
    'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
    'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
    'time' => timeformat($row['poster_time']),
    ),
)
}

AND this is just an example really, cause this array gets built within that function for EACH attachment, which goes into another array. Ofcourse there is MORE info. here than I need, but I will shrink it down later.

So, currently I am doing a foreach on the $attachments array to grab all of the attachments, and this gives me all of the information for the attachments as well. No problem with any of that.

The problem I am having is how to grab the information from the $attachments array in the order from the $attach_info array. I need the output to be what is in the attach_info array number-wise, in the order it is in.

For example, if $attach_info array is like this:

$attach_info(0,1,5,4);

Than it should output the Members name first, than the Filename, than the Time of the Attachment, and lastly a link to the post.

So the order of this array is important. And should grab this information from the $attachments array.

Can someone please help me with a way to set this up within a foreach loop so that the information gets echo'd in the correct order. And the attach_info array can change ofcourse. I'm looking for a fast approach to doing this. I'd rather not have to determine the order of the information and what information should be displayed everytime for each loop within the $attachments array if you know what I mean.

Thanks you guys Rock :)

EDIT

The $attachments array listed above is only 1 output of the actual array. It is actually outputting ALL attachments, so I need it to be able to LOOP through all attachments and still output the correct order and the correct information ONLY! Sorry if I failed to explain this.

$attachments array is 1 loop within an foreach... so it is just the output of only 1 foreach. I need them all.

So, what I can do now is this...

foreach($attachments as $item)
{
   echo 
 $item['member']['link'] . $item['file']['link'] . $item['file']['filesize'] . $item['file']['downloads'] . $item['topic']['link'] . $item['topic']['time'] . $item['file']['image']['link'];
}

BUT I need to echo the items in the correct order according to $attach_info array, and only echo those items!

Upvotes: 2

Views: 119

Answers (2)

ayk
ayk

Reputation: 1340

best of all try this:

foreach($attach_info as $val){
    switch($val){
        case 0: echo $attachments['member']['link'];
                break;
        case 1: echo $attachments['file']['link'];
                break;
        case 2: echo $attachments['file']['filesize'];
                break;
        case 3: echo $attachments['file']['downloads'];
                break;
        case 4: echo $attachments['topic']['link'];
                break;
        case 5: echo $attachments['topic']['time'];
                break;
        case 6: echo $attachments['file']['image']['link'];
                break;
    }
}

Upvotes: 2

Exta
Exta

Reputation: 904

May be it helps http://php.net/manual/ru/function.usort.php

Upvotes: 0

Related Questions