Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:18:19+00:00 2026-06-09T21:18:19+00:00

I’m surprised that there isn’t already an answer for this… it suggests to me

  • 0

I’m surprised that there isn’t already an answer for this… it suggests to me that I’m doing something fundamentally wrong. Or everyone else is.

Let’s say I have an array of possibly repeating values that were sorted client-side coming in as a POST request and the goal is to return a result-set from MySQL indexed by those values and in the same order as those values and preserving the redundancies!!!

So, if the values were: 1,2,3,2,3,4,5,5,5

The desired result is:

+----+-------+
| id | value |
+----+-------+
|  1 | one   |
|  2 | two   |
|  3 | three |
|  2 | two   |
|  3 | three |
|  4 | four  |
|  5 | five  |
|  5 | five  |
|  5 | five  |
+----+-------+

The code below seems to work, but Christ, seriously? I have to abuse array_intersect() and have two different loops, one of them nested, just to make a simple result set get ordered and repeated in the same pattern as the input array? This can’t be how it’s really supposed to be done.

function repQuery($inids,$incol,$querystring,$emptyval = "NULL"){
    # incol is the name of a column in the query
    # inids is an array within which the value of incol must be found
    # querystring is a query of the form...
    # "select FOO from BAR where %s in (%s) BLAH BLAH BLAH"
    # The first %s will be replaced by $incol and the second by $inids
    $qq = mysql_query(sprintf($querystring,$incol,implode(",",$inids))) or die(mysql_error());
    $nf = mysql_num_fields($qq); $dummyrow = array();
    for($ii = 0; $ii < $nf; $ii+= 1){
        $dummyrow[mysql_field_name($qq,$ii)] = $emptyval;
    };
    $out = array_fill(0,count($inids),$dummyrow);
    while($rr = mysql_fetch_assoc($qq)){
        foreach(array_keys(array_intersect($inids,array($rr[$incol]))) as $ii) {
            $out[(int)$ii] = $rr;
        }
    }
    return $out;
}

My question is: is there some accepted pattern or PHP array command or something else that I’m overlooking? In R the above would be a one-liner.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:18:20+00:00Added an answer on June 9, 2026 at 9:18 pm

    While it would be awesome if it were a feature in MySQL, I don’t think there is a way to use ORDER BY with a repeating pattern (you can get a “custom pattern” via ORDER BY FIELD(), but not quite there).

    I’m not sure about any special methods in PHP just for this, but using a temporary array might be able to reduce the headache:

    function repQuery($inids,$incol,$querystring,$emptyval = "NULL"){
        $qq = mysql_query(sprintf($querystring,$incol,implode(",",$inids))) or die(mysql_error());
    
        // build a temporary array of all of the results - the row's "ID" is the array index
        $tmpResults = array();
        while ($row = mysql_fetch_assoc($qq)) {
            $tmpResults[$row['id']] = $row;
        }
    
        // iterate through the order/list of IDs and insert each result one after the other
        $out = array();
        foreach ($inids as $id) {
            $out[] = $tmpResults[$id];
        }
        return $out;
    }
    

    This will place every SQL result (one for each ID) in the $tmpResults array using the row’s ID as the array’s index. The next step is to iterate through the $inids list, which contains the pattern to replicate, and to insert the corresponding record-result into the $out array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.