0

Here is a PHP array example

Array
(
    [A1345] => Array
        (
            [name] => Choco Chip Cookies
            [code] => A1345
            [price] => 180
            [quantity] => 1
            [mrp] => 220
        )

    [A1344] => Array
        (
            [name] => Handmade Soap
            [code] => A1344
            [price] => 210
            [quantity] => 1
            [mrp] => 258
        )

)

I would Like to use the [code] A1345 and A1344 as a different array, how can I do that. This is just an example. The array can be smaller or larger.

The intention is to save these product code in database when user submits the date via a shopping cart.

Which of the following is better ?

$names = array_column($array, 'code');
print_r($names);

#result Array ( [0] => A1345 [1] => A1344 )

or

    $result = array_map(
    function($x) {
        return $x['code'];
    }, $array);

print_r($result);

#Result Array ( [A1345] => A1345 [A1344] => A1344 )

  • 2
    "Better" in what way? – trincot Dec 17 '22 at 10:06
  • Also better compared to which existing Q&A material of the same matter that already exists on site? Please consider to research a little bit first, then clarify by [edit]. – hakre Dec 17 '22 at 10:35

0 Answers0