-1

I want to create and php associative array from an array like bellow

 array:3 [▼
  0 => array:7 [▼
    "item_id" => "1"
    "customer_id" => "53453"
    "name" => "Item3"
    "price" => 0
    "quantity" => 1
  ]
  1 => array:7 [▼
    "item_id" => "3"
    "customer_id" => "53453"
    "name" => "Item1"
    "price" => 0
    "quantity" => 1
  ]
  2 => array:7 [▼
    "item_id" => "2"
    "customer_id" => "765656"
    "name" => "Item2"
    "price" => 0
    "quantity" => 1
  ]
]

I want to create an assoc with customer_id from this array like bellow. My goal is to create an assoc array with common customer_id for get all customer itmes.

 array:2 [▼
  // if common customer id, make an item array for the customer
  53453=> array:2 [▼
   0 => array:3 [▼
      "quantity" => "1"
      "name" => "Item1"
      "price" => 0
    ],
   1 => array:3 [▼
      "quantity" => "1"
      "name" => "Item3"
      "price" => 0

    ]
  ]
  78640 => array:1 [▼
    0 => array:3 [▼
      "quantity" => "1"
      "name" => "Item2"
      "price" => 0
    ]
  ]
  
]
Syscall
  • 19,327
  • 10
  • 37
  • 52
mdkamrul
  • 274
  • 1
  • 13

2 Answers2

1

You can group by customer_id using the same key, and and the old array without the keys you don't want.

$array = [
  ["item_id" => "1", "customer_id" => "53453", "name" => "Item3", "price" => 0, "quantity" => 1],
  ["item_id" => "3", "customer_id" => "53453", "name" => "Item1", "price" => 0, "quantity" => 1],
  ["item_id" => "2", "customer_id" => "765656", "name" => "Item2", "price" => 0, "quantity" => 1],
];

$finalArray = [];

foreach ($array as $item)
{
    $customer_id = $item['customer_id'];
    unset($item['customer_id']);
    unset($item['item_id']);
    
    $finalArray[$customer_id][] = $item;
}

print_r($finalArray);

Outputs :

Array
(
    [53453] => Array
        (
            [0] => Array
                (
                    [name] => Item3
                    [price] => 0
                    [quantity] => 1
                )

            [1] => Array
                (
                    [name] => Item1
                    [price] => 0
                    [quantity] => 1
                )

        )

    [765656] => Array
        (
            [0] => Array
                (
                    [name] => Item2
                    [price] => 0
                    [quantity] => 1
                )

        )

)

online example

Or if you only want the create some selected keys :

$finalArray[$customer_id][] = [
    'name'     => $item['name'],
    'price'    => $item['price'],
    'quantity' => $item['quantity'],
];

online example

Syscall
  • 19,327
  • 10
  • 37
  • 52
0
$newarray=[];

foreach ($firstarray as $fa)
{
    $tarray=array($fa['qty'],$fa['name'],$fa['price']);
    $newarray[$fa['customer_id']][]=$tarray;
}

... and the result you want will be in $newarray, more or less.

Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39
  • but how if customer id is common? – mdkamrul Mar 21 '21 at 13:12
  • my goal is to make and final which identify a customer have which items – mdkamrul Mar 21 '21 at 13:14
  • From the snippet I posted the new array will be made up of arrays each with a key of the customer_id. Each array in that key will then be a full array with that customers items. – Gavin Simpson Mar 21 '21 at 13:14
  • https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value might assist you without having to create new arrays. You can search within an array or arrays. – Gavin Simpson Mar 21 '21 at 13:15