How do I add to the end of each sub array? Here is an example.
$products = array(
array( Code => 'TIR',
Description => 'Tires',
Price => 100
),
array( Code => 'OIL',
Description => 'Oil',
Price => 10
),
array( Code => 'SPK',
Description => 'Spark Plugs',
Price =>4
)
);
I want to add SKU=>1234 after Price in each array. Thanks
Loop across the array and use references to modify it:
foreach ($products as &$v) {
$v['SKU'] = 1234;
}
foreach ( $products as &$arr )
$arr['SKU'] = 1234;
Loop over the array using a reference (Note the ampersand before the $val
):
foreach ( $products as &$val ){
$val['SKU'] = 1234;
}
That way rather than $val
being a copy of the array element, it is a reference to the value, so altering it alters the value held in $products
.
foreach ($products as $k=>$v){
$v['SKU']=1234;
$products[$k]=$v;
}
print_r($products);