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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:40:15+00:00 2026-06-01T05:40:15+00:00

I have the following ‘key’ array: (Array 1) Array ( [0] => first_name [1]

  • 0

I have the following ‘key’ array: (Array 1)

Array
(
    [0] => first_name
    [1] => surname
    [2] => position
    [3] => age
)

This array should determine the keys and the order of keys/values which should exist in Array 2. So in a perfect world Array 2 would look like this:

Array
(
    [0] => Array
        (
            [first_name] => James
            [surname] => Johnstone
            [position] => Striker
            [age] => 42
        )

    [1] => Array
        (
            [first_name] => Al
            [surname] => MacLean
            [position] => Defender
            [age] => 22
        )

    ...

)

The problem I’m having is in the following example array. Sometimes:

a) the order of keys in Array 2 is not the same as Array 1

b) and some of the keys defined in Array 1 don’t exist in Array 2 – like so:

Array
(
    [0] => Array
        (
            [position] => Defender
            [first_name] => James
            [surname] => McDonald

        )

    [1] => Array
        (
            [position] => Striker
            [first_name] => Ben
            [surname] => Lailey
        )

    ...
)

I’d like some assistance creating a PHP function which will take a ‘badly formed’ Array 2 such as the one directly above, and convert it to how it should be: Order as defined by Array 1, and add any missing keys to become ‘correct’ like so:

Array
(
    [0] => Array
        (
            [first_name] => James
            [surname] => McDonald
            [position] => Defender
            [age] => 

        )

    [1] => Array
        (

            [first_name] => Ben
            [surname] => Lailey
            [position] => Striker
            [age] =>
        )

    ...
)

The keys used in this example are arbitrary, there could be a new key added, removed or re-ordered in Array 1 and I need Array 2 to respect Array 1.

Thank you in advance.

  • 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-01T05:40:17+00:00Added an answer on June 1, 2026 at 5:40 am

    A simple implementation:

    // Pass in an array of keys, and an array of arrays
    function cleanArray($keys, $arrays)
    {
        // Create an empty container for our final output
        $final = array();
    
        // Loop through array of arrays
        foreach($arrays as $a)
        {
            // Create empty value for current item
            $next = array();
    
            // Loop through keys, in order
            foreach($keys as $k)
            {
                // Assign next key and value if we have it, or a blank string if we don't
                $next[$k] = isset($a[k]) ? $a[k] : '';
            }
    
            // Add current item to output
            $final[] = $next;
        }
    
        // Return final values, each item now having its keys ordered and normalized
        return $final;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following url structure http://www.mydomain.com/vpn-offers-p3.html this page should be redirect to http://www.mydomain.com/vpn-offers.php?st=60&page=3 here
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I have following table it shows like this: ABC A B C 123 Hello
I have following problem: My program should decide at runtime to load an function
I have following url: http://localhost.com/phpdemo/bid/tf/red ? This url redirects through This [ $route['tf/red?'] =
I have following class. In this, Iris is another class with some attributes. public
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,

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.