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

  • Home
  • SEARCH
  • 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 8080801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:31:31+00:00 2026-06-05T16:31:31+00:00

Does anyone know a better (shorter/more elegant) way than simply writing a loop and

  • 0

Does anyone know a better (shorter/more elegant) way than simply writing a loop and building the array by hand to do the following:

I have an array called $data and another array called $indicies. The $indicies
array holds a bunch of indicies which I want to apply to the $data array to get
a subset out. For example if I ran array_rand with a number of elements greater
than 1, I would get an array of indicies out but I really want the array of data
items and would have to loop to build up that sub set.

I am thinking there might be some map reduce way to do this cleverly that I don’t know about. Anyone have any ideas? Here is an example of what I have to do now

$indicies = array_rand($data,6); //get 6 random indicies to the data
$subset = array();
foreach($indicies as $index)
    $subset[] = $data[$index];

here is something similar to what I would want to do

$subset = $data[array_rand($data,6)];
  • 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-05T16:31:33+00:00Added an answer on June 5, 2026 at 4:31 pm

    Didn’t test it, bu should work

    $result = array_intersect_key(
      $data,
      array_fill_keys($indices, null)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just out of curiosity, does anyone know a better way of building the following
Does anyone know a better way of getting the number of rows in a
Does anyone know a better way of displaying the contents of an activerecord result
There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way
Does anyone know, or better yet have an example, of a WCF service that
Does anyone know if there is a way to generate different code in the
Does anyone know of a way to contain a nonbreaking space in an html
Does anyone know of a good way to assert if a NameValueCollection is equivalent?
Does anyone know a better implementation of sprintf in PHP ? I was looking
does anyone know which is better for SEO rankings? Dynamic or Static? example: http://www.blah.com/this_page.php?id=1234

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.