2

I would like to compare lists with others in order to merge if their numbers (id_article) are the same. In my example, I would like to obtain this final array list :

*Final *

Array
(
    [0] => Array
        (
            [id_article] => 1
            [quantite] => 5
        )

    [1] => Array
        (
            [id_article] => 3
            [quantite] => 1
        )

}

INITIAL

Array
(
    [0] => Array
        (
            [id_article] => 1
            [quantite] => 2
        )

    [1] => Array
        (
            [id_article] => 1
            [quantite] => 3
        )

    [2] => Array
        (
            [id_article] => 3
            [quantite] => 1
        )

)

I tried with next() and current() but didn't work in this case.

Thanks in advance for help, advice or others examples.

3 Answers 3

2

You have to do some loop jobs:

$result = array();
foreach ($array as $value) {
    if (isset($result[$value['id_article']])) {
         $result[$value['id_article']] += $value['quantite'];
    } else {
         $result[$value['id_article']] = $value['quantite'];
    }  
}

$output = array();
foreach ($result as $id => $value) {
   $output[] = array('id_article' => $id, 'quantite' => $value);
}
Sign up to request clarification or add additional context in comments.

Comments

1

try this one

echo "<pre>";
$arr_input = array(
                array
                    (
                        "id_article" => 1,
                        "quantite" => 2
                    ),
                array
                    (
                        "id_article" => 1,
                        "quantite" => 3
                    ),
                array
                    (
                        "id_article" => 3,
                        "quantite" => 1
                    )
           );               

$arr_temp = array();    
foreach($arr_input as $arr)
{
    if(isset($arr_temp[$arr['id_article']]))
    {
        $arr_temp[$arr['id_article']] += $arr['quantite'];
    }
    else
    {
        $arr_temp[$arr['id_article']] = $arr['quantite'];
    }   
}   


$arr_ouput = array();
foreach($arr_temp as $key=>$val)
{
    $arr_output[] = array("id_article" => $key, "quantite" => $val);
}

print_r($arr_output);

Comments

0
$a = array(
    array(
        'id_article' => 1,
        'quantite' => 2,
    ),
    array(
        'id_article' => 1,
        'quantite' => 3,
    ),
    array(
        'id_article' => 3,
        'quantite' => 1,
    ),
);

$b = array();

foreach ($a as $k => $v) {
    if (array_key_exists($v['id_article'], $b)) {
        $b[$v['id_article']]['quantite'] += $v['quantite'];
    } else {
        $b[$v['id_article']] = $v;
    }
}

return array_values($b);

1 Comment

Thx @exec I use this solution for my project.

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.