Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Order info from 2 arrays
    text
    copied!<p>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...</p> <p>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...?)</p> <pre><code>$attach_info(0,1,5,4); </code></pre> <p>The numerical values here point to actual information to be displayed...</p> <ul> <li>0 = Members name (<code>$attachments['member']['link']</code>)</li> <li>1 = Filename (<code>$attachments['file']['link']</code>)</li> <li>2 = Filesize (<code>$attachments['file']['filesize']</code>)</li> <li>3 = Downloads (<code>$attachments['file']['downloads']</code>)</li> <li>4 = Link to post (<code>$attachments['topic']['link']</code>)</li> <li>5 = Time of attachment (<code>$attachments['topic']['time']</code>)</li> <li>6 = Image to display (<code>$attachments['file']['image']['link']</code>)</li> </ul> <p>The other array gets populated within a function and get RETURNED from that function with all of the information to be displayed...</p> <p>The returned information is this array:</p> <pre><code>while ($row = $smcFunc['db_fetch_assoc']($request)) { $attachments[$row['id_attach']] = array( 'member' =&gt; array( 'id' =&gt; $row['id_member'], 'name' =&gt; $row['poster_name'], 'link' =&gt; empty($row['id_member']) ? $row['poster_name'] : '&lt;a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '"&gt;' . $row['poster_name'] . '&lt;/a&gt;', ), 'file' =&gt; array( 'filename' =&gt; $filename, 'filesize' =&gt; round($row['filesize'] /1024, 2) . $txt['kilobyte'], 'downloads' =&gt; $row['downloads'], 'href' =&gt; $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'], 'link' =&gt; '&lt;img src="' . $settings['images_url'] . '/icons/clip.gif" alt="" /&gt; &lt;a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '"&gt;' . $filename . '&lt;/a&gt;', 'is_image' =&gt; !empty($row['width']) &amp;&amp; !empty($row['height']) &amp;&amp; !empty($modSettings['attachmentShowImages']), 'image' = array( 'id' =&gt; $id_thumb, 'width' =&gt; $row['width'], 'height' =&gt; $row['height'], 'img' =&gt; '&lt;img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '" /&gt;', 'thumb' =&gt; '&lt;img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" /&gt;', 'href' =&gt; $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image', 'link' =&gt; '&lt;a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image"&gt;&lt;img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '" /&gt;&lt;/a&gt;', ), ), 'topic' =&gt; array( 'id' =&gt; $row['id_topic'], 'subject' =&gt; $row['subject'], 'href' =&gt; $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'], 'link' =&gt; '&lt;a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '"&gt;' . $row['subject'] . '&lt;/a&gt;', 'time' =&gt; timeformat($row['poster_time']), ), ) } </code></pre> <p>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.</p> <p>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.</p> <p>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.</p> <p>For example, if <code>$attach_info</code> array is like this:</p> <p><code>$attach_info(0,1,5,4);</code></p> <p>Than it should output the Members name first, than the Filename, than the Time of the Attachment, and lastly a link to the post.</p> <p>So the order of this array is important. And should grab this information from the $attachments array.</p> <p>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.</p> <p>Thanks you guys Rock :)</p> <p><strong>EDIT</strong></p> <p>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.</p> <p>$attachments array is 1 loop within an foreach... so it is just the output of only 1 foreach. I need them all.</p> <p>So, what I can do now is this...</p> <pre><code>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']; } </code></pre> <p>BUT I need to echo the items in the correct order according to $attach_info array, and only echo those items!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload