0

I need help or advice on how to add or combine the value of array with same key.

For example:

Array(
    [price] => 123
    [category] => Fiction
    [bookname] => Any-Book
    [type] => Futuristic 
)
Array (
    [price] => 145
    [category] => Fantasy
    [bookname] => Any-Book
    [type] => Futuristic
)

Through many attempt I was not able to achieve the result I wanted it should be:

Array (
    [price] => 268
    [category] => Fantasy/Fiction
    [bookname] => Any-book
    [type] => Futuristic
)

I just need to combine the two they are essentially the same thing or needed to be bundled.

3
  • What is the same key? bookname? What if types are different? Commented Oct 11, 2016 at 15:36
  • switch cases based on the type of the 2 element Commented Oct 11, 2016 at 15:47
  • Will there ever be more than two arrays to combine? Will there ever be other key/value pairs? Commented Oct 11, 2016 at 15:56

2 Answers 2

1

You need to be sure of the type of your arrays and that they have the same keys :

 <?php
$a = array('price' => 123,
     'category' => 'Fiction',
     'bookname' =>'Any-book', 
     'type' => 'Futuristic');

      $b = array('price' => 145,
     'category' => 'Fantasy',
     'bookname' =>'Any-book', 
     'type' => 'Futuristic');



function mergeSameArray(array $a, array $b){
    $c = array();
    foreach($a as $key => $valueA){
        $typeA = gettype($valueA);
        $typeB = gettype($b[$key]); //$valueB
        if($typeB == $typeA){
            if($typeB == "double" || $typeA == "integer"){
                $c[$key] = $valueA+ $b[$key];
            }
            elseif($typeB == "string"){
                if($valueA == $b[$key]){
                    $c[$key] = $valueA;
                }
                else{
                    $c[$key] = $valueA.'/'. $b[$key];
                }
            }
            else{
                $c[$key] = "error nor string nor number";          
            }
        }
        else{
            $c[$key] = "error not same type";
        }

    }
    return $c;

}

    $arraySum = mergeSameArray($a,$b);
    print_r($arraySum);
Sign up to request clarification or add additional context in comments.

Comments

0

Here's my contribution, using array_walk, and making a few assumptions about the rules for the merge:

$a = Array(
    'price' => 123,
    'category' => 'Fiction',
    'bookname' => 'Any-Book',
    'type' => 'Futuristic'
);
$b = Array (
    'price' => 145,
    'category' => 'Fantasy',
    'bookname' => 'Any-Book',
    'type' => 'Futuristic'
);

array_walk ($a,
    function (&$v, $k, $comp) {
        if (is_int($v)) {
            $v = $v + $comp[$k];
        } else {
            if ($comp[$k] != $v) {
                $v = "$v/" . $comp[$k];
            }
        }
    }, $b);

var_dump($a);

The assumptions are: if the value in array $a is int, then sum it with the equivalent in $b. If the value is not int, compare it with the equivalent in $b, and if it matches leave it alone; otherwise concatenate wtih /.

2 Comments

Your solution is far more elegant than mine but you forgot float type
Thank you! we have identical codes! I messed up something on the condition.This is by far, the best answer. Thanks y'all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.