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 8379127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:05:44+00:00 2026-06-09T16:05:44+00:00

I have an array of post_ids $ids = array( [0] => 32 [1] =>

  • 0

I have an array of post_ids

$ids = array( 
    [0] => 32 
    [1] => 33 
    [2] => 21 
    [3] => 11 
    [4] => 13 
    [5] => 17 
    [6] => 41 
    [7] => 88 
    [8] => 92 
    [9] => 94 
    [10] => 96 
    [11] => 106 
    [12] => 117 
    [13] => 187 
    [14] => 220 
);

I want to get a column in database table where the post_id matches the ids in the above array. the table is structured like this

post_id | meta_key | meta_value

Currently, I’m using foreach ($ids as $id) to query a var one by one. This is inefficient with large data sets . Is there a way to query the meta_value column by the $ids array?

  • 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-09T16:05:45+00:00Added an answer on June 9, 2026 at 4:05 pm

    This is not the most efficient way but it works

    $idList = implode(",",$ids);
    

    now your query

    $query = "SELECT * FROM posts 
              WHERE post_id IN ($idList);";
    

    at least now it is reduced to one query

    Update:

    To secure from SQL injection when you populate your array make sure the your database input is safe:

    // if you are populating your array this way here is the trick
    $ids = array();
    foreach ($inputValues as $value){
       $ids[] = (int) $value; // this makes sure your input is an integer
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array full of post IDs like $post_id = array(3,56,89,98); Now what
I have an array with the ids of x cakes and another associative array
I have an array of ids and an array of corresponding values on a
I have this php code $ids = array(1,2,3); $names = array(cat,elephant,cow); $originalSettings = array
I have some certain categories' ids. I want to loop this categories and last
I have the below checkboxes and I need to get them as an array
I have this array $test = $_POST['test']; print_r($test); Array ( [0] => Array (
I have the following code: $postcode = $form->createElement('text', 'postcode'); $postcode->setLabel('Post code:'); $postcode->addValidator('regex', false, array('/^[a-z]{1,3}[0-9]{1,3}
I have a Form element: $Form=new Zend_Form; $Form->setAction($this->view->url(array('controller'=>'auth','action'=>'create'),null,true)) ->setMethod('post') ->setAttrib('id','auth-form') ->removeAttrib('enctype'); As can be
I have array INPUTFILES with n files INPUTFILES=( file_0 ... files_n-1 ) And i

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.