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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:56:04+00:00 2026-06-01T23:56:04+00:00

I have two large arrays: $a = array(‘a1′,’a2′,’a3′,’a4’); // and so on $b =

  • 0

I have two large arrays:

$a = array('a1','a2','a3','a4'); // and so on
$b = array('b1'=>'a1', 'b2'=>'a3', 'b3'=>'a1'); // 

I would like to get the following result:

$a = array('a1'=>array('b1', 'b3'), 'a2'=>array(), 'a3'=>array('b2') );

I could just do:

foreach($a as $aa){
    foreach($b as $bb){
        // check with if then add to a
    }
}

but it would get tremendously large with bigger numbers.

So it occurred to me that if i remove each ‘b’ element after being added to $a the next loops will be smaller, and i would cut on resources.

However when splicing the looped array, the index does not seem to get updated, and the next loop does not take into consideration that fact that it was cut down by 1.

How can I make this work, and also, is there a better way of fitting items of an array into the appropriate indexes of another array?

EDIT:

how would this be done if the structure of both $a and $b were:

$a[0]['Word']['id']=1;
$a[0]['Word']['sentence_id']=2;
$a[0]['Word']['word_string']='someWord';

$b[0]['Word']['id']=3;
$b[0]['Word']['sentence_id']=4;
$b[0]['Word']['word_string']='someWord';

// And i would like to list `b` like so:
$a[0]['list_of_bs']=array(b[0], b[1]);
//So that i can get:
echo $a[0]['list_of_bs']['Word']['id'];
// and to get result 3

and i would like it to be $a[0][Word][list_of_b]=array(b1,b2,b3) and each of the b‘s has it’s own data in associative 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-01T23:56:06+00:00Added an answer on June 1, 2026 at 11:56 pm

    Try this,

    $a = array('a1','a2','a3','a4');
    $b = array('b1'=>'a1', 'b2'=>'a3', 'b3'=>'a1');
    
    foreach($a as $values)
    {
        $key = array_keys($b, $values);
    
        $new_array[$values] = $key;
    }
    

    $new_array -> will be the result that you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays First array has large number of words, i have 2nd
I have lines of code with two large arrays (so can't just write it
I have two arrays, one is very large (more than million entries) and other
I have two arrays. One is a larger bit of data: Array ( [12]
I have an array full of dynamic <select> field options, that I would like
I have two arrays: symbols - smaller array fetchedSymbolsArray - larger array How can
I have two large identical-sized files. One is ASCII plain text, and the other
I have two large (~100 GB) text files that must be iterated through simultaneously.
I have two large source trees. One of them has some out of date
I have two large tables in MySQL, one containing about 6,00,000 records and another

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.