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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:22:23+00:00 2026-06-05T19:22:23+00:00

I just found out how to loop an array based on another array, but

  • 0

I just found out how to loop an array based on another array, but my problem is that if the second array is just 1 object than it works fine but I want to make it work with two objects.

Here is an example how it do works,

$shorten = array(
  0 => 'ECAR',
  1 => 'CCAR',
  2 => 'ICAR',
  3 => 'SCAR',
  4 => 'FCAR',
  5 => 'PCAR',
);

$data = array(
  'Hertz' => array(
        'ECAR' => '49.21',
        'CCAR' => '71.04',
        'ICAR' => '89.58',
        'SCAR' => '100.00',
    )
  ),
  'Avis' => array(
        'ECAR' => '412.00',
        'CCAR' => '347.00',
        'ICAR' => '285.00',
        'SCAR' => '224.00',
        'FCAR' => '165.00',
        'PCAR' => '100.00',
    )
  ),
);

// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));

foreach($data as &$array) {
    // merge to get set members
    $array = array_merge($shorten, $array);
}
unset($array);

print_r($data);

But I want to make it work with this one,

$shorten = array(
  0 => 'ECAR',
  1 => 'CCAR',
  2 => 'ICAR',
  3 => 'SCAR',
  4 => 'FCAR',
  5 => 'PCAR',
);

$data = array(
  'Hertz' => array(
    'NYCT01' => array(
        'ECAR' => '49.21',
        'CCAR' => '71.04',
        'ICAR' => '89.58',
        'SCAR' => '100.00',
    )
  ),
  'Avis' => array(
    'NYCT01' => array(
        'ECAR' => '412.00',
        'CCAR' => '347.00',
        'ICAR' => '285.00',
        'SCAR' => '224.00',
        'FCAR' => '165.00',
        'PCAR' => '100.00',
    )
  ),
);

// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));

foreach($data as $firstArray) {
    foreach($firstArray as &$array){
        // merge to get set members
        $array = array_merge($shorten, $array);
    }
}
unset($array);

print_r($data);

And this is what I want it to be,

Array
(
    [Hertz] => Array
        (
            [ECAR] => 49.21
            [CCAR] => 71.04
            [ICAR] => 89.58
            [SCAR] => 100.00
            [FCAR] => n/a
            [PCAR] => n/a
        )

    [Avis] => Array
        (
            [ECAR] => 412.00
            [CCAR] => 347.00
            [ICAR] => 285.00
            [SCAR] => 224.00
            [FCAR] => 165.00
            [PCAR] => 100.00
        )

)
  • 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-05T19:22:25+00:00Added an answer on June 5, 2026 at 7:22 pm
    foreach($data AS $company => $nyc){
        $inner = $nyc['NYCT01'];
        foreach($shorten AS $car){
            if(array_key_exists($car, $inner)){
                $output[$company][$car] = $inner[$car];
            }else {
                $output[$company][$car] = "n/a";
            }
        }
    }
    

    Just loop through the data and check if the key of any of the cars exists inside the NYCT01 array, if it does set the value, else, “n/a”.

    My output:

    Array
    (
        [Hertz] => Array
            (
                [ECAR] => 49.21
                [CCAR] => 71.04
                [ICAR] => 89.58
                [SCAR] => 100.00
                [FCAR] => n/a
                [PCAR] => n/a
            )
    
        [Avis] => Array
            (
                [ECAR] => 412.00
                [CCAR] => 347.00
                [ICAR] => 285.00
                [SCAR] => 224.00
                [FCAR] => 165.00
                [PCAR] => 100.00
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just found out that lazy loading in Entity Framework only works from the
Just found out that the video output of the iPad is not a system
I just found out that in FF, if you are dynamically creating an OPTION
I've just found out that there is a possibility to host a Windows Forms
I've just found out about View States in Flex (v3.0), but I am not
PHP5 introduced an object-oriented file wrapper, SplFileObject . I guess I just found out
Something that really would like to know but never found out are shortcuts in
I just found out about ie7-js ; IE7 is a JavaScript library to make
I just found out about superfish and currently using it on an asp.net. The
I just found out about Alice and Scratch. I will be implementing those pretty

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.