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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:26:29+00:00 2026-06-06T16:26:29+00:00

Assume a master array which is already sorted in ASCENDING order: $values = array(‘value1’,

  • 0

Assume a master array which is already sorted in ASCENDING order:

$values = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');

And another array with the keys(indexes) being ordered per some specific requirement.

$keys = array(0, 2, 1, 5);

Required Logic: should create an array say $output

  • beginning with elements of $values who’s indexes are stored $keys preserving the order of indexes.
  • remaining elements of $values shall be appended in the rear in ascending order.

e.g. 1

$values = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');
$keys = array(0, 2, 1, 5);
$output = array('value1', 'value3', 'value2', 'value6', 'value4', 'value5');

e.g. 2

$values = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');
$keys = array(5);
$output = array('value6', 'value1', 'value2', 'value3', 'value4', 'value5');

e.g. 3

$values = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');
$keys is empty, no keys(indexes).
$output = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');

I’ve $values and $keys arrays. I just need to figure out how to create $output array. I’m pretty sure there will need to be a foreach loop on the $values array for this to work.

I’m running into a wall here trying to figure this thing out…

  • 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-06T16:26:31+00:00Added an answer on June 6, 2026 at 4:26 pm

    Try this…

    Basically parsing the $values array and pulling the relevant value out.
    Unsetting the array as we go to leave only the values which were not sorted.
    The remaining items in the array will be merged after the loop. with array_merge.

    Unsetting the array doesn’t change the index values, which is useful in this situation.

    There will be errors if there are more $keys than $values though. Need to add some error checks.

    $values = array('value1', 'value2', 'value3', 'value4', 'value5', 'value6');
    $keys = array(0, 2, 1, 5);
    
    foreach ($keys AS $key) {
        $tempOutput[] = ($values[$key])?$values[$key]:'';
        unset($values[$key]);
    }
    
    $output = array_merge($tempOutput, $values);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 'master' branch and several topic branches. Assume that the master branch
Assume I've declared a function (or class, doesn't matter) in a header file, which
Assume that I've got a project with two branches. One is master and the
Let's assume I have an array of object properties I'd like to access: $properties
Assume the following simple table design: Table Master (Id bigint PK, Message nvarchar(50)) Sample
Let's assume we have X applications – one 'master' and 2+ 'slaves'. Now, slaves
Can I use an Array of Dictionary objects? I have an XML which I
Let assume that I have a relation Master Slave. In the database exists one
Assume I have two tables, Student Test Id Name TestId Type StudentId -- ----
Assume I have an enumerable object enum and now I want to get the

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.