Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have this array:
Array(
[0] => Array(
[id] => 1,
[value] => 100
)
[1] => Array(
[id] => 1,
[value] => 200
)
[2] => Array(
[id] => 1,
[value] = 300
)
[3] => Array(
[id] => 2,
[value] = 100
)
[4] => Array(
[id] => 2.
[value] => 200
)
)
How can I count sum of [values]'s of the elements with the same [id] and put to array like:
Array(
[1] => 600,
[2] => 300
)
?
THANKS!
$array=array(array('id'=>1,'value'=>100),array('id' => 1,'value' => 200),array('id' => 1,'value' => 300),array('id' => 2,'value' => 100),array('id' => 2,'value'=> 200));
$res=array();
foreach($array as $a)
{
if(array_key_exists ($a['id'],$res ))
{
$res[$a['id']]+=$a['value'];
}
else
{
$res[$a['id']]=$a['value'];
}
}
print_r($res);
output
Array ( [1] => 600 [2] => 300 )
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Our Result:
Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female )
)
I want a result looking like this: How to convert like this?
array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)
OR
array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)
Pretty simple using an array_combine() to set the headers for each row, and just walking the array setting those headers:
$headers = array_shift($myArray);
array_walk(
$myArray,
function(&$row) use ($headers) {
$row = array_combine($headers, $row);
}
);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I fetch an array from API using
<?php
$json = json_decode(file_get_contents("API_URL"), TRUE);
echo '<pre>';
print_r($json);
?>
I got that result like that
Array
(
[deals] => Array
(
[0] => Array
(
[activeDa
te] => 1430332361000
[bogo] =>
[categories] => Array
(
[0] => Array
(
[id] => 29057
[name] => Bath & Body
)
[1] => Array
(
[id] => 20733
[name] => Cosmetics
)
[2] => Array
(
[id] => 29190
[name] => Skin Care
)
[3] => Array
(
[id] => 20856
[name] => Fragrances
)
[4] => Array
(
[id] => 29059
[name] => Beauty & Personal Care
)
)
[clearance] =>
[couponCode] => HAPPYMOM
[dealImageUrl] => http://cdn.savings.com/logo/1737578.png
[dealUrl] => http://www.savings.com/m/p/19561077/8306099/c?afsrc=1&up=2015-05-01-05-15
[description] => Go through this link to get Assorted Spring Getaway tote for only $20 on orders $40 or more, save 80%. Restrictions may apply. Limited time offer only or when supplies run out.
[discount] => 1
[exclusive] =>
[freeShipping] =>
[homePageStaffPick] =>
[id] => 3862713
[lastUpdated] => 1430332361000
[merchantDisplayUrl] => http://www.bathandbodyworks.com
[merchantId] => 236514
[merchantImageUrl] => http://cdn.savings.com/logo/1737578.png
[merchantName] => Bath and Body Works
[merchantPageStaffPick] =>
[merchantScore] => 17
[merchantUrl] => http://www.savings.com/m/p/19561077/1742990/c?afsrc=1
[minimumSpend] => 0.00
[mobileMonetized] =>
[monetized] => 1
[printable] =>
[promotion] => 80% Off
[rebate] =>
[scope] => SITE_WIDE
[score] => 579
[siteUrls] => Array
(
[0] => http://www.bathandbodyworks.com
)
[startDate] => 1430290800000
[tip] =>
[title] => Get 80% off Assorted Spring Getaway Tote on Orders Over $40 - Only $20
[validated] =>
[voteDown] => 0
[voteUp] => 0
)
My question is how i get the value of [deals][categories][0][name] ?
I want to store value of categories name.
You have your answer in your question itelf. just a little modifiction needed. Please try this:-
echo $yourarrayname['deals'][0]['categories'][0]['name'];
Note:-since category is on the Zero th index of deals. So put zero index before category index.
You already have your answer. The only thing you need to do is make them literal strings and take the first element in the deals array. So:
echo $json['deals'][0]['categories'][0]['name']
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have array
Array
(
[0] => Array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[cluster_name] => Personal Impact
)
)
how to create new array become
Array
(
[Personal Impact] => Array
(
[Position] => array
(
[0] => array
(
[question_summary] =>
[answer1] => 18
[answer2] => 3
[PercentEVQ] => 10.8000
[PercentEVQ2] => 11
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
[1] => Array
(
[question_summary] =>
[answer1] => 51
[answer2] => 3
[PercentEVQ] => 30.6000
[PercentEVQ2] => 31
[driver_display_name] => Position
[driver_name] => Position
[cluster_name] => Personal Impact
)
)
)
)
Is it possible?
Yes it's possible you can just foreach(your values as key => value) {} it or something like this:
Sample Fiddle
<?php
$new_values = array();
foreach($old_values as $key => $values) {
$new_values[$values['cluster_name']][$values['driver_display_name']][] = array(
'answer1' => $values['answer1'],
'answer2' => $values['answer2'],
'PercentEVQ' => $values['PercentEVQ'],
'PercentEVQ2' => $values['PercentEVQ2'],
'driver_display_name' => $values['driver_display_name'],
'cluster_name' => $values['cluster_name'],
);
}
Are you defining a new array?
$new_array['Personal Impact']['Position'] = $old_array;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array:
Array
(
[0] => Array
(
[id] => 31299
[name] => 37322426212
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322426212
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[1] => Array
(
[id] => 4373
[name] => 37322983029
[ips] =>
[tech_prefix] =>
[password] =>
[id_voip_hosts] =>
[proxy_mode] =>
[auth_type] => ani
[ani] => 37322983029
[accname] =>
[protocol] =>
[port] =>
[orig_enabled] => 1
[term_enabled] =>
[orig_capacity] =>
[term_capacity] =>
[orig_rate_table] => 7
[term_rate_table] =>
[id_dr_plans] =>
[orig_groups] =>
[term_groups] =>
[notes] =>
)
[2] => Array
(
[auth_type] => ani
[name] => 37322983029
[ani] => 37322983029
[orig_enabled] => Array
(
[0] => on
)
[orig_rate_table] => 7
)
)
Items with 0,1,2 may be more(3,4...10...and so on).
What I am trying to do is to find array with key ani and name = 37322983029 and replace
name with '###' and ani to empty:
[mane]=>"###",//where name = '37322983029'
[ani]=> //where ani = '37322983029'
I tried with str_replace but no success.
How can this be done?
I think, this is what you are looking for:
foreach($yourArray as &$a)
{
if($a["ani"] == 37322983029 and $a["ani"] == $a["name"])
{
$a["name"] = "###";
$a["ani"] = "";
}
}
Not sure how you were using str_replace, but the following should work (assuming that ani and mane (or name?) are actually string indices into your array - not obvious from your example):
foreach($myArray as &$val) {
if($val['ani'] == 37322983029) {
$val['ani'] = '';
$val['name'] = "###";
}
}
important Note the use of & to pass the value by reference so you are working with actual elements of the array, not copies... In that way, when you modify $val you are actually modifying the original array. Without the ampersand, your initial array would be unchanged by the loop.
Also note - it is not clear from your example whether you want to test for just ani==37322983029 or also for name=37322983029. I am sure you can modify the code above to have both conditions (or see Alexxus's answer)
A complete code example (tested, working):
<?php
$a = Array(Array('a'=>4, 'b'=>5),Array('a'=>6, 'b'=>7));
echo '<pre>';
print_r($a);
foreach($a as &$v){
if($v['a']==6) {
$v['a'] = 12;
$v['b'] = '';
}
}
print_r($a);
echo '</pre>';
?>
Produces output
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 6
[b] => 7
)
)
Array
(
[0] => Array
(
[a] => 4
[b] => 5
)
[1] => Array
(
[a] => 12
[b] =>
)
)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have created an array:
Array
(
[0] => Array
(
[product_price] => 88.0000
[category_id] => 13
)
[1] => Array
(
[product_price] => 180.0000
[category_id] => 13
)
[2] => Array
(
[product_price] => 180.0000
[category_id] => 13
)
[3] => Array
(
[product_price] => 150.0000
[category_id] => 13
)
[4] => Array
(
[product_price] => 200.0000
[category_id] => 14
)
)
The above array will be create only tow index(0 and 1). Here I want to remove remove duplicate arrays category_id wise and will add all duplicate value's product_price will be add.I want to like as :
Array
(
[0] => Array
(
[product_price] => 598.0000
[category_id] => 13
)
[1] => Array
(
[product_price] => 200.0000
[category_id] => 14
)
)
try this code:
Here $cart is your array name
$res_arr = array();
foreach($cart as $p)
{
$found = false;
foreach($res_arr as $k => $r)
{
if($r["category_id"]==$p["category_id"])
{
$res_arr[$k]["product_price"] += $p["product_price"];
$found = true;
}
}
if(! $found)
$res_arr[] = $p;
}
print_r($res_arr);
Create a map with category_id as key. Define custom insert method for map which checks that if key already exists then add product_price to old one
Here is the code-
<?php
$arr=array(
0 => array
(
'product_price' => 88.0000,
'category_id' => 13
),
1 => array
(
'product_price' => 180.0000,
'category_id' => 13
),
2 => array
(
'product_price' => 180.0000,
'category_id' => 13
),
3 => array
(
'product_price' => 150.0000,
'category_id' => 13
),
4 => array
(
'product_price' => 200.0000,
'category_id' => 14
)
);
//logic here
$cat_arr=array();
$reduced_arr=array();
//Fetching unique category_id.
foreach($arr as $sub_arr)
{
if(!in_array($sub_arr['category_id'], $cat_arr))
$cat_arr[]=$sub_arr['category_id'];
}
//Getting the final result
foreach($cat_arr as $cat)
{
$price=0;
foreach($arr as $prod)
{
if($prod['category_id']==$cat)
$price+=$prod['product_price'];
}
$reduced_arr[]= array('product_price' => $price, 'category_id' => $cat);
}
print_r($reduced_arr);
?>
O/P
Array ( [0] => Array ( [product_price] => 598 [category_id] => 13 ) [1] => Array ( [product_price] => 200 [category_id] => 14 ) )