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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:44:07+00:00 2026-06-09T21:44:07+00:00

I have two arrays, $A and $B. Both arrays have the same length, but

  • 0

I have two arrays, $A and $B. Both arrays have the same length, but array $A will always either have the same number of non-empty elements as $B or it will have less non-empty elements than $B.

So if,

$B = array("john","adams","sandwich");

then $A could either be,

$A = array("bacon","ham","juice");

or it could be,

$A = array("bacon","","");

I’m trying to match the empty elements of $B to $A. So I can get,

$B = array("john","","");

I’m currently doing it through the following code:

$q = count($A) - 1;
$l = count($A) - count(array_filter($A));
$i = 1;
while($i <= $l){
    $B[$q] = "";
    $i++;
    $q--;
}

But this method is extremely slow and might cause problems considering that I’m going to be doing it several times. Do you guys have any suggestions on how to do this more efficiently? The arrays will never be larger than 20 elements, but this is still time consuming for some reason.

  • 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-09T21:44:08+00:00Added an answer on June 9, 2026 at 9:44 pm

    Following code will work in all cases, whether empty values are on any position in array:

    $A = array('a', 'b', 'c');
    $B = array('', 'ab', 'zx');
    $B = array_filter($B, 'strlen');
    $B = array_filter($A, 'customfun');
    print_r($B);
    exit;
    function customfun($param) {
        global $A;
        global $B;
        $key = array_search($param, array_values($A));
        if(isset($B[$key])) {
            return true;
        } else {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays values and keys both of the same length. I want
I have two JavaScript arrays below that both have the same number of entries,
I have two arrays idxListResponse & _index both of which have the same structure.
I have two string arrays Array1[size] and Array2[size]. They both have the same size.
I have two arrays of products, both formatted exactly the same, like so: $products
I have two arrays... $Name = array(a, b, c, d); $Value = array(1, 2,
I have two arrays. Example of the first array: $arrayOne = array ( 'fruit'
I have two arrays each array has some values for instance: int a[] =
I have two arrays of doubles of the same size, containg X and Y
let's say I have two arrays like so: $array1 = array('A' => array( 'B'

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.