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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:27:08+00:00 2026-06-01T16:27:08+00:00

PHP’s array is very flexible and useful. I counted over 30 array functions on

  • 0

PHP’s array is very flexible and useful. I counted over 30 array functions on the PHP.net array reference page. Some of them can solve my problem, but I’m looking for the best, most elegant way.

I have 2 arrays, called labor and cost, each containing a table:

labor = array(
   0 => array('date' => date, 'labor'=> labor), 
   1 => array('date' => date, 'labor'=> labor),
   ...
);

cost = array(
   0 => array('date' => date, 'cost'=> cost), 
   1 => array('date' => date, 'cost'=> cost),
   ...
);

My problem is that sometimes the number of dates don’t match (i.e., there are days when you’ve incurred costs, even though you spent nothing on labor, or days when you had labor but no cost) – that means there are more lines in one array then the next – no way to know which has without using count().

What I’m interested in are only the days that had both labor and cost and I want to end up with an array:

laborcost = array(
   0 => array('date' => date, 'labor'=> labor, 'cost' => cost), 
   1 => array('date' => date, 'labor'=> labor, 'cost' => cost),
   ...
)

I thought about using array_intersect() or one of the ‘u’ functions, but ended totally mixed up. Before giving up and writing my own array scanning function, I wanted to see if there are any ideas that will solve my issue with 1, possibly 2, lines of code.

  • 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-01T16:27:10+00:00Added an answer on June 1, 2026 at 4:27 pm

    There’s is no intersect function accepting a user-defined comparison function that allows you to modify the arrays. The simplest way is just to do it yourself.

    Here are a few examples:

    O(2n + m)

    // Remap dates as keys for faster lookups
    $result = $nlabor = $ncost = array();
    foreach ($labor as $l) $nlabor[$l['date']] = $l;
    foreach ($cost as $c) $ncost[$c['date']] = $c;
    
    // Compare
    foreach ($nlabor as $date => $l) {
        if (array_key_exists($date, $ncost)) {
            $result[] = array_merge($l, $ncost[$date]);
        }
    }
    

    ~O(n * m)

    // Just compare them all
    $result = array();
    foreach ($labor as $l) {
        foreach ($cost as $c) {
            if ($l['date'] == $c['date']) {
                $result[] = array_merge($l, $c);
                break;
            }
        }
    }
    

    Which way is the best depends on how many elements you have in each array. When used on smaller arrays ~O(n * m) is fine, while on bigger arrays O(2n + m) will be more efficient.

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

Sidebar

Related Questions

PHP's explode function returns an array of strings split on some provided substring. It
PHP treats all arrays as associative, so there aren't any built in functions. Can
PHP, as we all know is very loosely typed. The language does not require
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined
----- PHP and mySQL ----- I have two quick questions need some advice. On
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
PHP Fatal error: Default value for parameters with a class type hint can only
PHP has built in support for reading EXIF and IPTC metadata, but I can't
PHP funcitons are written in C and you can look at the source code
php> $a = array(a=>1,b=>0,c=>1,d=>1,e=>0); php> $b = array(); php> foreach ($a as $k =>$v){

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.