Changes quantities that are displayed in a shopping cart page
You don't do a good job of validating $_POST['pid']. You just assume the value passed will be good (a bad mistake when working with user input).

May I suggest something like:

$filterOptions = array('options' =&gt; array('min_range' =&gt; 1));
$pid = filter_input(INPUT_POST, 'pid', FILTER_VALIDATE_INT, $filter_options);
if(is_null($pid) || $pid === false) {
     // parameter was either not set (null) or failed filter (false)
     // you should handle this somehow
     exit();
}




The two different code examples are not the same in that the cart array is build differently in each.  The first would build a numerically-indexed structure at $_SESSION['cart_array'] like this:

[
    0 =&gt; [
        'item_id' =&gt; {{pid}},
        'quantity' =&gt; {{quantity}}
    ],
    1 =&gt; [
        ...
    ],
    ...
]


Whereas the second would build an array index by item id like this:

[
    {{pid}} =&gt; [
        'item_id' =&gt; {{pid}},
        'quantity' =&gt; {{quantity
    ], 
    {{pid2}} =&gt; [
        ...
    ],
    ...
]


Did you really intend to change this structure? If so, are there other areas of code that might need to change to accommodate this changed structure?




$result = print_r(array_keys('cart_array',$pid))



This makes no sense. cart_array is not an array suitable for use with array_keys().  Why would you use print_r() (an output function) here at all?

If you want to use the second form of the cart data shown above, this is how you would extract pid values from it:

$pidsInCart = array_keys($_SESSION['cart_array']);


With the numerically-indexed cart array, it might be:

$pidsInCart = array_map(
    function($item) {
        return $item['item_id'];
    },
    $_SESSION['cart_array']
);


I would also would be careful in evaluating the result directly, as you could get unexpected truthy/falsey results there I might use:

$pidsInCart = ...;
if(!empty($pidsInCart)) {
     ...
}




Also, use meaningful variable names. $result here is a good example of a not very meaningful name in this context.



Indent your code properly.