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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:00:36+00:00 2026-06-11T07:00:36+00:00

I am learning to program in php and I have a little problem. I

  • 0

I am learning to program in php and I have a little problem.
I have two arrays:
Array A:

array(10){
           [0]: string(10) "2012-08-01"
           [1]: int 1
           [2]: string(10) "2012-08-03"
           [3]: int 1
           [4]: string(10) "2012-08-07"
           [5]: int 2
           [6]: string(10) "2012-08-09"
           [7]: int 2
           [8]: string(10) "2012-08-27"
           [9]: int 1
          }

Array B:

array(4){
          [0]: string(10) "2012-09-04"
          [1]: string(10) "2012-09-06"
          [2]: string(10) "2012-09-08"
          [3]: string(10) "2012-09-10"
        }

I want to check if the first element from Array A equals the first element from Array B, if it does then write second element (in this case 1) from array A to an new Array C (one dimensional) else don’t write anything in an array C and next check if element with index 2 from an array A equals second element [1]: string(10) "2012-09-06" from an array B if so the write fourth element ([3]: int 1) from an array A to an array C etc. so an array C would have int elements only.
If someone could help me out how to sort this out please I would be very happy

  • 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-11T07:00:38+00:00Added an answer on June 11, 2026 at 7:00 am

    If you really need to do it this way, you could do something like the following:

    foreach($arrayB as $bRow)
    {
        if(FALSE !== ($key = array_search($bRow, $arrayA))
        {
            $arrayC[] = $arrayA[$key+1];
        }
    }
    

    However, a string-indexed array would probably be better for Array A. Use the following for the 3 examples below:

    $arrayA = array("2012-08-01" => 1, "2012-08-03" => 1, "2012-08-07" => 2, "2012-08-09" => 2, "2012-08-27" => 1);
    $arrayB = array("2012-09-04", "2012-09-06", "2012-09-08", "2012-09-10");
    $arrayC = array();
    

    If Array A always contains more elements than Array B (or the same number):

    foreach($arrayA as $key=>$value)
    {
        if(in_array($key, $arrayB))
        {
            $arrayC[] = $value;
        }
    }
    

    If Array B always contains more elements than Array A (or the same number):

    foreach($arrayB as $bRow)
    {
        if(isset($arrayA[$bRow]))
        {
            $arrayC[] = $arrayA[$bRow];
        }
    }
    

    If it is possible that sometimes Array A is longer and sometimes Array B is longer:

    $matchedIndexes = array();
    
    foreach($arrayA as $key=>$value)
    {
        if(in_array($key, $arrayB))
        {
            $arrayC[] = $value;
            $matchedIndexes[] = $key;
        }
    }
    
    foreach($arrayB as $bRow)
    {
        // Only add the entries that we did not already add when looping through $arrayA
        if(!in_array($key, $matchedIndexes) && isset($arrayA[$bRow]))
        {
            $arrayC[] = $arrayA[$bRow];
        }
    }
    

    Populating arrayC with zeros foreach entry in arrayA that does not have a match in arrayB:

    foreach($arrayA as $key=>$value)
    {
        if(in_array($key, $arrayB))
        {
            $arrayC[] = $value;
        }
        else
        {
            $arrayC[] = 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a weird problem. I'm learning PHP (used to program in Java), and
I'm learning how to program for the first time, (with Ruby), because I want
I'm new to learning php and in one of my first programs I wanted
I'm just learning to program...I have a dropdownlist like this. <div> <asp:DropDownList ID=DropDownList1 runat=server>
I am amateur programmer learning how to program. I have never had any computer
I'm learning OpenCV because I want to build and program a 3D Scanner over
Ok so I've just started learning java (I usually program in Objective-C). My first
I will try to explain my problem clearly: I have two tables: document and
I have some php and javascript experience and just started learning curl. Been messing
I want to make a tech news website. I started learning PHP, and since

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.