-4

am looking for a way to achieve result of the above title. I have a two dimensional array like this

Array
(
    [0] => Array
        (
            [reference_no] => A0012
            [code] => HSWH30
            [net_unit_price] => 24000
            [quantity] => 2
        )

    [1] => Array
        (
            [reference_no] => A0012
            [code] => HSWH15
            [net_unit_price] => 21000
            [quantity] => 2
        )

    [2] => Array
        (
            [reference_no] => A0013
            [code] => HS-106AR
            [net_unit_price] => 2400
            [quantity] => 1
        )

    [3] => Array
        (
            [reference_no] => A0013
            [code] => HS-8012
            [net_unit_price] => 4500
            [quantity] => 2
        )
)

And am looking for a way to compare arrays using the [reference_no] value, then add arrays with same [reference_no] in inside an array, thereby forming a three dimensional array as shown below

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [reference_no] => A0012
                    [code] => HSWH30
                    [net_unit_price] => 24000
                    [quantity] => 2
                )

            [1] => Array
                (
                    [reference_no] => A0012
                    [code] => HSWH15
                    [net_unit_price] => 21000
                    [quantity] => 2
                )
        )

    [1] => Array
        (
            [0] => Array
                (
                    [reference_no] => A0013
                    [code] => HS-106AR
                    [net_unit_price] => 2400
                    [quantity] => 1
                )

            [1] => Array
                (

                    [reference_no] => A0013
                    [code] => HS-8012
                    [net_unit_price] => 4500
                    [quantity] => 2
                )       
        )
)

Please I need a quick help, have tried using for loop and array_push to compare and merge/combine similar array, but am not getting the result that I need.

Gifto
  • 21
  • 1
  • 2
  • 9

3 Answers3

0

Why not try lets say:

 $arrayExample[0] => Array
                ( [reference_no] => A0013
                        [code] => HS-8012
                         [net_unit_price] => 4500
                         [quantity] => 2 )

so technically speaking the value of $arrayExample[0]["reference_no"] is A0013 if so why not use

if( strpos($arrayExample[0]["reference_no"],"A0013") !==false)
{
    $arrayExample[0]["reference_no"] = array( 
[0] = //something
[1] = //something1
...
)}

Hope it helps

cybeeer
  • 3
  • 3
  • The reference_no value is dynamic, don't see this helping. – Gifto Oct 29 '18 at 05:48
  • `$arrayExample = Array ( 'reference_no' => 'A0012', 'code' => 'HSWH30', 'net_unit_price' => 24000, 'quantity' => 2 ); echo $arrayExample['reference_no']; $arrayExample['reference_no'] = array('newData'=>12345, 'exampleData'=>56789); echo ''; echo $arrayExample['reference_no']['newData']; echo '
    '; echo $arrayExample['reference_no']['exampleData']; $arrayExample['reference_no']['exampleData'] = array('anotherData' => 'something'); echo '
    '; echo $arrayExample['reference_no']['exampleData']['anotherData']; `
    – cybeeer Oct 29 '18 at 07:31
0

I would create a new associative array, and use the reference_no as the keys. Then it's much easier to check if the key exists in the new array.

$sorted = [];

foreach($array as $el) {
  $ref = $el[reference_no];
  if (array_key_exists($ref, $sorted)) {
    $sorted[$ref][] = $el;
  } else {
    $sorted[$ref] = [$el];
  }
}

If you needed to convert $sorted to have a sequential numeric index (0,1,2,3 ...), you could do that as a final step with array_map, or another foreach loop.

Brian Wagner
  • 817
  • 1
  • 6
  • 11
0

All you need to do is to loop the array and create a new associative array with the ref no as the key.
This will group the items as you want, and when the loop is done you reset the index of the array with array values.

foreach($arr as $sub){
    $new[$sub['reference_no']][] = $sub;
}
$new = array_values($new);
var_dump($new);

Output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0012"
      ["code"]=>
      string(6) "HSWH30"
      ["net_unit_price"]=>
      string(5) "24000"
      ["quantity"]=>
      string(1) "2"
    }
    [1]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0012"
      ["code"]=>
      string(6) "HSWH15"
      ["net_unit_price"]=>
      string(5) "21000"
      ["quantity"]=>
      string(1) "2"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0013"
      ["code"]=>
      string(8) "HS-106AR"
      ["net_unit_price"]=>
      string(4) "2400"
      ["quantity"]=>
      string(1) "1"
    }
    [1]=>
    array(4) {
      ["reference_no"]=>
      string(5) "A0013"
      ["code"]=>
      string(7) "HS-8012"
      ["net_unit_price"]=>
      string(4) "4500"
      ["quantity"]=>
      string(1) "2"
    }
  }
}

https://3v4l.org/DQEmC

Andreas
  • 23,610
  • 6
  • 30
  • 62