david2000 david2000 - 2 months ago 6
PHP Question

expdend array on same value

i have an array like this:

Array
(
[0] => Array
(
[item_id] => 18
[seller_id] => 1
[item_name] => test1
[price] => 10.00
[username] => user
[ship_price] => 10.00
)

[1] => Array
(
[item_id] => 16
[seller_id] => 1
[item_name] => test2
[price] => 10.00
[username] => user
[ship_price] => 20.00
)


i get this array after foreach :

foreach($result as $i => $row){

$row[$i]["item_id"] = $row["PRODUCTS_ITEM_ID"];
$row[$i]["seller_id"] = $row["PRODUCTS_SELLER_ID"];
$row[$i]['item_name'] = $row['PRODUCTS_ITEM_NAME'];
$row[$i]['price'] = $row['PRODUCTS_PRICE'];
$row[$i]['username'] = $row['username'];
$row[$i]['ship_price'] = $row['ship_price'];

}


How can i expdend the array if the "seller_id" is the same,
i just want to group all the data that are belong to the "seller_id" in the same array key.

i want to get something like this: (sorry if i have an error)

Array
(
["seller_id"] => Array
(
[0] => Array
[item_id] => 18
[seller_id] => 1
[item_name] => test1
[price] => 10.00
[ship_price_value] => 10.00
[username] => user
[ship_price] => 10.00

[1] => Array
[item_id] => 16
[seller_id] => 1
[item_name] => test2
[price] => 10.00
[ship_price_value] => 10.00
[username] => user
[ship_price] => 20.00
)


)

Answer

You have 2 Options on how to solve this. The First would be to generate the desired Array Structure within your initial foreach()Loop. This means you have to do this only once. The Code for this option would look something like the Code Snippet below.

NOTE: The Key seller_id is not added as a string; instead, the seller_id in the Key is simply the numeric seller_id for items that have the similar seller_ids. This was intentional because, if you tried to use the string-key seller_id and you have items with different seller_ids; you can be sure of Data-Loss since subsequent Data would override previous ones — the Keys being static, identical and the same [seller_id]: so keep that in mind as well...

<?php

    $arrSellerRows              = [];

    foreach($result as $i => $row){
        $temp                       = [];
        $sellerID                   = $row["PRODUCTS_SELLER_ID"];
        $temp[$i]["item_id"]        = $row["PRODUCTS_ITEM_ID"];
        $temp[$i]["seller_id"]      = $sellerID;
        $temp[$i]['item_name']      = $row['PRODUCTS_ITEM_NAME'];
        $temp[$i]['price']          = $row['PRODUCTS_PRICE'];
        $temp[$i]['username']       = $row['username'];
        $temp[$i]['ship_price']     = $row['ship_price'];

        if(!array_key_exists($sellerID, $arrSellerRows)){
            $arrSellerRows[$sellerID]   = [];
            $arrSellerRows[$sellerID][] = $temp;
        }else{
            $arrSellerRows[$sellerID][] = $temp;
        }
    }

The Second option would require that you take the resulting Array from your initial foreach() Loop and then process it a little further to get the desired Structure. This would not be efficient since it implies using 2 Loops for something that could be achieved with just one, single Loop:

<?php


    $array      = [
        [
            'item_id'       => 18,
            'seller_id'     => 1,
            'item_name'     => 'test1',
            'price'         => 10.00,
            'username'      => 'user',
            'ship_price'    => 10.00,
        ],
        [
            'item_id'       => 16,
            'seller_id'     => 1,
            'item_name'     => 'test2',
            'price'         => 10.00,
            'username'      => 'user',
            'ship_price'    => 20.00,
        ],
    ];


    function groupBySellerID($array){
        $arrSellerRows  = [];
        foreach($array as $iKey=>$arrData){
            $sellerID   = $arrData['seller_id'];
            if(!array_key_exists($sellerID, $arrSellerRows)) {
                $arrSellerRows[$sellerID]   = [];
                $arrSellerRows[$sellerID][] = $arrData;
            }else{
                $arrSellerRows[$sellerID][] = $arrData;
            }
        }
        return $arrSellerRows;
    }

    var_dump( groupBySellerID($array) );
    //YIELDS:::
    array (size=1)
      1 => // THIS NUMBER REPRESENTS THE SELLER_ID (WHICH IS 1)
        array (size=2)
          0 =>
            array (size=6)
              'item_id' => int 18
              'seller_id' => int 1
              'item_name' => string 'test1' (length=5)
              'price' => float 10
              'username' => string 'user' (length=4)
              'ship_price' => float 10
          1 =>
            array (size=6)
              'item_id' => int 16
              'seller_id' => int 1
              'item_name' => string 'test2' (length=5)
              'price' => float 10
              'username' => string 'user' (length=4)
              'ship_price' => float 20