How to set value in array of array index - php

Array
(
[a31df20a1e75a9200fc0d8b136438c91] => Array
(
[addons] => Array
(
[0] => Array
(
[name] => Add Message here, First 5 Words Free
[value] => tyyty tytyt ty
[price] => 10
)
)
[product_id] => 1356
[variation_id] =>
[variation] =>
[quantity] => 1
[line_total] => 110
[line_tax] => 0
[line_subtotal] => 110
[line_subtotal_tax] => 0
)
)
how can i set value of "[line_total] => 110"?

$thearray['a31df20a1e75a9200fc0d8b136438c91']['line_total'] = 110;

You know the path to the value, so you can just set it..
$array['a31df20a1e75a9200fc0d8b136438c91']['line_total'] = 12345;

Related

Group Simple Array PHP

Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.
This is my array:
Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)
[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)
[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)
[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)
[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)
As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:
Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)
[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)
[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)
You can do this with a simple foreach loop:
$array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);
$out = [];
foreach($array as $value){
$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}
print_r($out);
Output
Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)
[38] => Array
(
[size] => 38
[quantity] => 0
)
[39] => Array
(
[size] => 39
[quantity] => 3
)
)
Sandbox
I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.
//reset the array keys
$out = array_values($out);
print_r($out);
Output
Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)
[1] => Array
(
[size] => 38
[quantity] => 0
)
[2] => Array
(
[size] => 39
[quantity] => 3
)
)
Sandbox
Cheers

how do i get array element isMultiple in php

hi there i am new to php and i want to access isMultiple element of array how can i do that then i want to show if the value of isMultiple is 1 then it should show yes.. any help would be appreciated and it would be great......!!.
Array
(
[result] => Array
(
[0] => Array
(
[pkFeatureTypeId] => 72
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[1] => Array
(
[pkFeatureTypeId] => 32
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[2] => Array
(
[pkFeatureTypeId] => 33
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[3] => Array
(
[pkFeatureTypeId] => 35
[Name] =>
[Status] => 1
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
)
)
Try this.
foreach($data['result'] as $item){
if($item['isMultiple'] == '1'){
echo 'Yes';
}
}

remove array on duplicate value

I need help. I want to remove array on array list when there is duplicate value.
I have this array
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
As you may notice that there are duplication value on price key. But I only want to remove an array if the the count is equal to 0. But if the duplicate price has no value on count. It only remove one duplicate array. My expected output will be like this.
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
Please help thanks.

Array - how to replace record with newly added

I got the array below and I would like to keep recent added record which is [2] and delete others
Array (
[0] => Array ( [id] => 1 [is_sub] => 0 [product] => New [quantity] => 1 [price] => [total_item_price] => 0 [comments] => )
[1] => Array ( [id] => 1 [is_sub] => 0 [product] => Old [quantity] => 1 [price] => [total_item_price] => 0 [comments] => )
[2] => Array ( [id] => 4 [is_sub] => 0 [product] => Mix [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) )
i got an answer thanks for helping.
$last = array_slice($orders, -1, 1, true);
Shorter:
$last = array_pop($orders);
$last_data = array_pop($data);
print_r($last_data);

Store a variable from the array of object php

I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.

Categories