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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:25:53+00:00 2026-05-28T15:25:53+00:00

I have taken a long time researching how to perform this operation between arrays

  • 0

I have taken a long time researching how to perform this operation between arrays but I can not get an algorithm in PHP I return these results.

Array #1:

Array DB_ITEMS
(
    [1] => Array
        (
            [item_code] => FO1321
            [item_quantity] => 5
            [item_sellprice] => 18.00
            [found] => 0
        )

    [2] => Array
        (
            [item_code] => HE240
            [item_quantity] => 1
            [item_sellprice] => 22.40
            [found] => 0
        )

)

Array #2:

Array BUY_ITEMS
(
    [1] => Array
        (
            [item_code] => FO1321
            [item_quantity] => 1
            [item_sellprice] => 18.00
            [taken] => 0
        )

    [2] => Array
        (
            [item_code] => EL55
            [item_quantity] => 1
            [item_sellprice] => 8.00
            [taken] => 0
        )

)

I want this Result, in an Array Format:

Array FINAL_RESULT
(
    [1] => Array
        (
            [item_code] => FO1321
            [item_quantity] => -4
            [item_sellprice] => 22.40
            [taken] => 0
        )

    [2] => Array
        (
            [item_code] => HE240
            [item_quantity] => -1
            [item_sellprice] => 22.40
            [taken] => 0
        )

    [3] => Array
        (
            [item_code] => EL55
            [item_quantity] => +1
            [item_sellprice] => 8.00
            [taken] => 0
        )

)

I’m doing this to compare an existing bill that is being modified.
And I need to set the differences between them and then make changes in DB.

  • 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-05-28T15:25:53+00:00Added an answer on May 28, 2026 at 3:25 pm

    As you did not provide too much logic (only for quantities)…

      $DB_ITEMS = array(
        array(
            'item_code' => 'FO1321',
            'item_quantity' => 5,
            'item_sellprice' => 18,
            'found' => 0
        ),
        array(
            'item_code' => 'HE240',
            'item_quantity' => 1,
            'item_sellprice' => 22.4,
            'found' => 0
        )
       );
    
      $BUY_ITEMS = array(
        array(
            'item_code' => 'FO1321',
            'item_quantity' => 1,
            'item_sellprice' => 18,
            'taken' => 0
        ),
        array(
            'item_code' => 'EL55',
            'item_quantity' => 1,
            'item_sellprice' => 8,
            'taken' => 0
        )
       );
    
      // fast fix
      $db = array(); $buy = array();
      foreach($DB_ITEMS as $item)
        $db[$item['item_code']] = $item;
      foreach($BUY_ITEMS as $item)
        $buy[$item['item_code']] = $item;
      unset($DB_ITEMS, $BUY_ITEMS);  
    
      // now deal with arrays
      foreach($db as $code=>$item)
        if (array_key_exists($code, $buy))
             $buy[$code]['item_quantity'] -= $item['item_quantity'];
        else
        {
             $buy[$code] = $item;
             $buy[$code]['item_quantity'] =- $item['item_quantity'];
             $buy[$code]['taken'] = $buy[$code]['found'];
             unset($buy[$code]['found']);
        }
    
      // output result
      var_dump($buy);
    

    Result:

    array(3) {
      ["FO1321"]=>
      array(4) {
        ["item_code"]=>
        string(6) "FO1321"
        ["item_quantity"]=>
        int(-4)
        ["item_sellprice"]=>
        int(18)
        ["taken"]=>
        int(0)
      }
      ["EL55"]=>
      array(4) {
        ["item_code"]=>
        string(4) "EL55"
        ["item_quantity"]=>
        int(1)
        ["item_sellprice"]=>
        int(8)
        ["taken"]=>
        int(0)
      }
      ["HE240"]=>
      array(4) {
        ["item_code"]=>
        string(5) "HE240"
        ["item_quantity"]=>
        int(-1)
        ["item_sellprice"]=>
        float(22.4)
        ["taken"]=>
        int(0)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program, that takes a long time to load. Because of this,
I have this code for Jaro-Winkler algorithm taken from this website. I need to
How can i get the exact time taken by lucene to search a string.
I have been stuck on this for a long time and for some reason
I have a program which takes a long time to complete. I would like
We have noticed that component publishing takes a long time. The component we are
I have a PHP process that takes a long time to run. I don't
I have a task that takes a rather long time and should run in
I have a long running method which can take a minute. On my local,
I have taken this little snippet straight out of some code I'm working on:

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.