I have an array that looks like this...
Array
(
[0] => Array
(
[code] => AS34
[2014-12-10] => 32
)
[1] => Array
(
[code] => AS34
[2014-12-11] => 42
)
[2] => Array
(
[code] => AS34
[2014-12-12] => 40
)
[3] => Array
(
[code] => AS34
[2014-12-15] => 44
)
[4] => Array
(
[code] => AH98
[2014-12-10] => 1
)
[5] => Array
(
[code] => AT78
[2014-12-12] => 1
)
[6] => Array
(
[code] => AL44
[2014-12-10] => 23
)
[7] => Array
(
[code] => AL44
[2014-12-11] => 27
)
[8] => Array
(
[code] => AL44
[2014-12-13] => 25
)
[9] => Array
(
[code] => AL44
[2014-12-15] => 26
)
)
I am trying to turn it into an array that looks like this...
var $example_data = array(
array(
'ID' => 1,
'code' => 'AS34',
'09/12/14' => '0',
'10/12/14' => '32',
'11/12/14' => '42',
'12/12/14' => '40',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '44',
),
array(
'ID' => 2,
'code' => 'AH98',
'09/12/14' => '0',
'10/12/14' => '1',
'11/12/14' => '0',
'12/12/14' => '0',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 3,
'code' => 'AT78',
'09/12/14' => '0',
'10/12/14' => '0',
'11/12/14' => '0',
'12/12/14' => '1',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 4,
'code' => 'AL44',
'09/12/14' => '0',
'10/12/14' => '23',
'11/12/14' => '27',
'12/12/14' => '0',
'13/12/14' => '25',
'14/12/14' => '0',
'15/12/14' => '26',
),
);
So basically it sets up an array for each 'code' and then the previous 7 days. Can anybody point me in the direction of a similar example or some reading on the right method I should be looking at using?
I figured this:
$data = Array
(
0 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-10" => 32
),
1 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-11" => 42
),
2 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-12" => 40
),
3 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-15" => 44
),
4 => Array
(
"id" => 1,
"code" => "AH98",
"2014-12-10" => 1
),
5 => Array
(
"id" => 1,
"code" => "AT78",
"2014-12-12" => 1
),
6 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-10" => 23
),
7 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-11" => 27
),
8 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-13" => 25
),
9 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-15" => 26
)
);
function in_array_r($needle, $haystack, $strict = false) { //taken from http://stackoverflow.com/a/4128377/4263082
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
$codes = Array();
$counter = -1;
foreach($data as $key => $value) {
if(!in_array_r($data[$key]["code"], $codes, true)) {
$codes[++$counter] = Array();
foreach($data[$key] as $subkey => $subvalue) {$codes[$counter][$subkey] = $subvalue;}
}
else{
foreach($data[$key] as $subkey => $subvalue) {
if($subkey != "code" && $subkey != "id") {$codes[$counter][$subkey] = $subvalue;}
}
}
}
print_r($codes);
OUTPUT
Array
(
[0] => Array
(
[id] => 1
[code] => AS34
[2014-12-10] => 32
[2014-12-11] => 42
[2014-12-12] => 40
[2014-12-15] => 44
)
[1] => Array
(
[id] => 1
[code] => AH98
[2014-12-10] => 1
)
[2] => Array
(
[id] => 1
[code] => AT78
[2014-12-12] => 1
)
[3] => Array
(
[id] => 1
[code] => AL44
[2014-12-10] => 23
[2014-12-11] => 27
[2014-12-13] => 25
[2014-12-15] => 26
)
)
Something like (fixed):
$example_data = array();
foreach($data as $id => $row) {
$code = $row['code'];
unset($row['code']);
$karr = array_keys($row);
$date = current($karr);
$example_data[$code]['ID'] = $id;
$example_data[$code]['code'] = $code;
$example_data[$code][$date] = $row[$date];
}
print_r($example_data);
Test online
I got this working:
<?php
header ('Content-type: text/plain; charset=utf-8');
//source data from the question
$sourceArr = array(
array('id' => 1
,'code' => 'AS34'
,'2014-12-10' => '32'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-11' => '42'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-12' => '40'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-15' => '44'
)
,array('id' => 1
,'code' => 'AH98'
,'2014-12-10' => '1'
)
,array('id' => 1
,'code' => 'AT78'
,'2014-12-12' => '1'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-10' => '23'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-11' => '27'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-13' => '25'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-15' => '26'
)
);
foreach($sourceArr as $k => $v)
{
//get dates for last seven days
$d0 = date('Y-m-d',time() - 60 * 60 * 24 * 0);
$d1 = date('Y-m-d',time() - 60 * 60 * 24 * 1);
$d2 = date('Y-m-d',time() - 60 * 60 * 24 * 2);
$d3 = date('Y-m-d',time() - 60 * 60 * 24 * 3);
$d4 = date('Y-m-d',time() - 60 * 60 * 24 * 4);
$d5 = date('Y-m-d',time() - 60 * 60 * 24 * 5);
$d6 = date('Y-m-d',time() - 60 * 60 * 24 * 6);
//if we have a valid element - then save it in temp array
if(array_key_exists($d0,$v)){ $tmpArr[$v['code']][$d0] = "'$v[$d0]'"; }
if(array_key_exists($d1,$v)){ $tmpArr[$v['code']][$d1] = "'$v[$d1]'"; }
if(array_key_exists($d2,$v)){ $tmpArr[$v['code']][$d2] = "'$v[$d2]'"; }
if(array_key_exists($d3,$v)){ $tmpArr[$v['code']][$d3] = "'$v[$d3]'"; }
if(array_key_exists($d4,$v)){ $tmpArr[$v['code']][$d4] = "'$v[$d4]'"; }
if(array_key_exists($d5,$v)){ $tmpArr[$v['code']][$d5] = "'$v[$d5]'"; }
if(array_key_exists($d6,$v)){ $tmpArr[$v['code']][$d6] = "'$v[$d6]'"; }
}
//create the result array
$cnt = 0;
foreach($tmpArr as $k => $v)
{
$resultArr[$cnt]['ID'] = $cnt;
$resultArr[$cnt]['code'] = "'$k'";
asort($v);
foreach($v as $k2 => $v2)
{
$resultArr[$cnt][$k2] = $v2;
}
$cnt++;
}
//output
echo "Old array: ".print_r($sourceArr,1);
echo "New array: ".print_r($resultArr,1);
?>
Related
Php multidimensional array same key’s same value’s related total in
new array. I have an array of following mentioned. i need new array
as total qty of same item_id. anyone can help would be appreciate.
My Original Array is as following
Array
(
[a] => Array
(
[item] => Array
(
[item_id] => 1
)
[qty] => 0
),
[b] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 35
),
[c] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 15
),
[e] => Array
(
[item] => Array
(
[item_id] => 3
)
[qty] => 20
),
);
I want array Output like following :
Array(
[0] => Array (
[item_id] => 1,
[item_total_qty] => 0,
)
[1] => Array (
[item_id] => 2,
[item_total_qty] => 50,
)
[2] => Array (
[item_id] => 3,
[item_total_qty] => 20,
)
);
Hope it help
$arrays = array(
'a' => array(
'item' => array(
'item_id' => 1
),
'qty' => 0
),
'b' => array(
'item' => array(
'item_id' => 2
),
'qty' => 35
),
'c' => array(
'item' => array(
'item_id' => 2
),
'qty' => 15
),
'd' => array(
'item' => array(
'item_id' => 3
),
'qty' => 20
)
);
$result = array();
foreach ($arrays as $key => $array) {
if (is_array($result) && !empty($result)) {
foreach ($result as $key => $r) {
if ($r['item_id'] == $array['item']['item_id']) {
$result[$key]['item_total_qty'] += $array['qty'];
continue 2;
}
}
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
} else {
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
}
}
Simple foreach on your original table:
$sorted = array();
foreach ($original as $item) {
$id = $item['item']['item_id'];
$sorted[$id]['item_total_qty'] = $sorted[$id] ? $sorted[$id] + $item['qty'] : item['qty'];
$sorted[$id]['item_id'] = $id;
}
$sorted = array_values($sorted);
I have two multi-dimensional associative array ,
first we have
Array
(
[user_authentication] => Array
(
[api_user_id] => xxxxxxxxxxxxxxxxxxxxxxxx
[api_auth_token] => xxxxxxxxxxxxxxxxxxxxxx
)
[campaign_details] => Array
(
[campaign_name] => democampaign
[campaign_category] => appsGames
[campaign_sub_category] => Action
[campaign_type] => cpc
[campaign_start_date] => MM/DD/YYYY
[campaign_end_date] => MM/DD/YYYY
[campaign_start_time] => HH:mm
[campaign_end_time] => HH:mm
)
[campaign_budget_info] => Array
(
[campaign_daily_budget] => 0.2
[campaign_hourly_budget] => 0.3
[campaign_bid] => 0.1
[campaign_budget] => 1
)
[campaign_targetting_info] => Array
(
[campaign_os_type] => Apple
[country_code] => IN,AF,AG
[state_id] => Array
(
[IN] => 1,2,3
[AF] => 4,5,6
[AG] => 7,8,9
)
[carrier] => Array
(
[IN] => Tata,Aircel,RCOM,Vodafone,Airtel,Idea Cellular,Uninor,Dishnet,BSNL
[AF] =>
[AG] =>
)
[isp] =>
[device_targeting] => iphone,ipad
[conversion] =>
)
[campaign_creative_info] => Array
(
[campaign_domain] => abcd.com
[campaign_click_url] => http://url-to-redirect-users-to-after-they-click.com/
[campaign_banner_size] => URL640x1136
[campaign_banner_url] => http://imageurl.com/
[campaign_creative_type] => image
)
[campaign_black_list_white_list_info] => Array
(
[black_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
[black_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
[black_list_ip_addresses] => 123.123.12.123,10.100.100.100
[white_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
[white_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
[white_list_ip_addresses] => 123.123.12.123,10.100.100.100
)
)
and second one is which i have make to compare with
Array
(
[user_authentication] => Array
(
[api_user_id] => 1
[api_auth_token] => 1
)
[campaign_details] => Array
(
[campaign_name] => 1
[campaign_category] => 1
[campaign_sub_category] => 1
[campaign_type] => 1
[campaign_start_date] => 1
[campaign_end_date] => 1
[campaign_start_time] => 1
[campaign_end_time] => 1
)
[campaign_budget_info] => Array
(
[campaign_daily_budget] => 1
[campaign_hourly_budget] => 1
[campaign_bid] => 1
[campaign_budget] => 1
)
[campaign_targetting_info] => Array
(
[campaign_os_type] => 1
[country_code] => 1
[state_id] => Array
(
[IN] => 1
[AF] => 1
[AG] => 1
)
[carrier] => Array
(
[IN] => 1
[AF] => 1
[AG] => 1
)
[isp] => 1
[device_targeting] => 1
[conversion] => 1
)
[campaign_creative_info] => Array
(
[campaign_domain] => 1
[campaign_click_url] => 1
[campaign_banner_size] => 1
[campaign_banner_url] => 1
[campaign_creative_type] => 1
)
[campaign_black_list_white_list_info] => Array
(
[black_list_app_ids] => 1
[black_list_device_ids] => 1
[black_list_ip_addresses] => 1
[white_list_app_ids] => 1
[white_list_device_ids] => 1
[white_list_ip_addresses] => 1
)
)
we have to compare the array and find which key is missing in first array
i have tried this but not working
$comparemodel= array_diff_assoc($array1,$array2);
if($comparemodel==0){
echo "hello";
}
else{
$keys = array_keys($comparemodel);
for ($i = 0; $i < count($keys); $i++) {
$error_message[] = $keys[$i] . " is missing";
}
$model = array();
$errors = array("error_code" => 3042, "error_message" => $error_message);
$message = $error_message;
$status = 0;
$finalarray = array("modal" => $model, "errors" => $errors, "message" => $message, "status" => $status);
echo json_encode($finalarray);
}
its not working with this associative array but its working with simple array. what should i do for this.
thanks
try this code
<?php
$arr1=array("campaign_details" => array
(
"campaign_name" => "democampaign",
"campaign_category" => "appsGames",
"campaign_sub_category" => "Action",
"campaign_type" => "cpc",
"campaign_start_date" => "MM/DD/YYYY",
"campaign_end_date" => "MM/DD/YYYY",
"campaign_start_time" => "HH:mm",
"campaign_end_time" => "HH:mm"
),
"campaign_budget_info" => array
(
"campaign_daily_budget" => 0.2,
"campaign_hourly_budget" => 0.3,
"campaign_bid" => 0.1,
"campaign_budget" => 1,
),
"campaign_targetting_info" => array
(
"campaign_os_type" => "Apple",
"country_code" => "IN,AF,AG",
"state_id" => array
(
"IN" => "1,2,3",
"AF" => "4,5,6",
"AG" => "7,8,9"
),
"carrier" => Array
(
"IN" => "Tata,Aircel,RCOM,Vodafone,Airtel,Idea
Cellular,Uninor,Dishnet,BSNL",
"AF" => "",
"AG" => "",
),
"isp" => "",
"device_targeting" => "iphone,ipad",
"conversion" => "",
),
"campaign_creative_info" => array
(
"campaign_domain" => "abcd.com",
"campaign_click_url" => "http://url-to-redirect-users-to-after-
they-click.com/",
"campaign_banner_size" => "URL640x1136",
"campaign_banner_url" => "http://imageurl.com/",
"campaign_creative_type" => "image",
),
"campaign_black_list_white_list_info" => array
(
"black_list_app_ids" =>
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
"black_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
"black_list_ip_addresses" => "123.123.12.123,10.100.100.100",
"white_list_app_ids" =>
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
"white_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
"white_list_ip_addresses" => "123.123.12.123,10.100.100.100",
)
);
$arr2=array("campaign_details" =>array
(
"campaign_name" => 1,
"campaign_category" => 1,
"campaign_sub_category" => 1,
"campaign_type" => 1,
"campaign_start_date" => 1,
"campaign_end_date" => 1,
"campaign_start_time" => 1,
"campaign_end_time" => 1
),
"campaign_budget_info" => array
(
"campaign_daily_budget" => 1,
"campaign_hourly_budget" => 1,
"campaign_bid" => 1,
"campaign_budget" => 1,
),
"campaign_targetting_info" => array
(
"campaign_os_type" => 1,
"country_code" => 1,
"state_id" => array
(
"IN" => 1,
"AF" => 1,
"AG" => 1,
),
"carrier" => array
(
"IN" => 1,
"AF" => 1,
"AG" => 1,
),
"isp" => 1,
"device_targeting" => 1,
"conversion" => 1,
),
"campaign_creative_info" =>array
(
"campaign_domain" => 1,
"campaign_click_url" => 1,
"campaign_banner_size" => 1,
"campaign_banner_url" => 1,
"campaign_creative_type" => 1,
),
"campaign_black_list_white_list_info" => array
(
"black_list_app_ids" => 1,
"black_list_device_ids" => 1,
"black_list_ip_addresses" => 1,
"white_list_app_ids" => 1,
"white_list_device_ids" => 1,
"white_list_ip_addresses" => 1,
)
);
function array_keys_multi(array $array)
{
$keys = array();
foreach ($array as $key => $value) {
$keys[] = $key;
if (is_array($array[$key])) {
$keys = array_merge($keys, array_keys_multi($array[$key]));
}
}
return $keys;
}
$resArr=array();
$a=array_keys_multi($arr1);
$b=array_keys_multi($arr2);
$c=array_diff($a,$b);
if(count($c) > 0){
echo "There is differnce<br/>";
echo "<pre>";
print_r($c);
}else
echo "There is no differnce<br/>";
?>
I am trying to update the below array to set "duplicate" => true when both "name" and "date" are the same. In the below example 'array[1][duplicate]=>true' as both
array[0] & array[1] have the same "name"=john & "date"=2015-7-24
Array
(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[2] => Array
(
[id] => 1
[name] => jane
[date] => 2015-07-24
[duplicate] => false
)
[3] => Array
(
[id] => 1
[name] => notJaneORJohn
[date] => 2015-07-24
[duplicate] => false
)
[4] => Array
(
[id] => 1
[name] => jane
[date] => 2099-07-24
[duplicate] => false
)
)
Try this,
$array = Array
(
0 => Array
(
'id' => '1',
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false',
),
1 => Array
(
'id' => 1,
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false'
),
2 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2015-07-24',
'duplicate' => 'false'
),
3 => Array
(
'id' => 1,
'name' => 'notJaneORJohn',
'date' => '2015-07-24',
'duplicate' => 'false'
),
4 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2099-07-24',
'duplicate' => 'false'
)
);
foreach ($array as $key => $value) {
for ($i = $key + 1 ; $i < sizeof($array); $i++) {
if ($value['name'] === $array[$i]['name'] && $value['date'] === $array[$i]['date']) {
$array[$key]['duplicate'] = 'TRUE';
$array[$i]['duplicate'] = 'TRUE';
}
}
}
This would work:
$Arr = Array(
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'Jane', 'date'=>'2015-07-24', 'duplicate'=>0]
);
foreach($Arr as $i1 => $v1){
$Str1 = $v1['name'].$v1['date'];
foreach($Arr as $i2 => $v2){
if( $i1 !== $i2 && $Str1 === $v2['name'].$v2['date'] ){
$Arr[$i1]['duplicate'] = 1;
}
}
}
echo '<pre>',print_r($Arr),'</pre>'; die();
... outputs:
Array(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[2] => Array
(
[id] => 1
[name] => Jane
[date] => 2015-07-24
[duplicate] => 0
)
)
Have a look at this shortcut method ;)
<?php $testarr = array(
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "notJaneORJohn","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2099-07-24","duplicate" => "false")
);
$tempArray = array();
function checkDuplicate(&$arr) {
global $tempArray;
if (count($tempArray) > 0 && in_array($arr['name'], $tempArray) && in_array($arr['date'], $tempArray)) {
$arr['duplicate'] = "true";
} else {
$tempArray[] = $arr['name'];
$tempArray[] = $arr['date'];
}
}
array_walk($testarr, 'checkDuplicate');
print_r($testarr);
I have been trying to get the code to output the data in a certain format so i can use it in a graph chart, and would like to get some help in the final stages. Thanks in advance.
This is my code that produces the below array.
$monthly_sales_array = array();
$i=0;
foreach($ord as $sales)
{
$month_is = date('m-Y',$sales->order_date);
$monthly_sales_array['months'][$month_is][$i] = $sales->qty*$sales->price_per;
$i++;
}
Array
(
[months] => Array
(
[07-2014] => Array
(
[0] => 33
[1] => 33
[2] => 26
[3] => 26
[4] => 38.5
[5] => 33
[6] => 165
)
[06-2014] => Array
(
[21] => 0.01
[22] => 44
[23] => 48
)
)
)
Trying to get this outcome:
Array(
[months] => Array
(
[07-2014] => 354.5
[06-2014] => 92.01
)
)
Try something like this :
$monthly_sales_array = array();
$my_array = array(
'months' => array(
'07-2014' => array(
'0' => '33',
'1' => '33',
'2' => '26',
'3' => '26',
'4' => '38.5',
'5' => '33',
'6' => '165' ,
),
'06-2014' => array(
'21' => '0.01',
'22' => '44',
'23' => '48',
)
)
);
foreach ($my_array['months'] as $key => $value) {
$sum = array_sum(array_map(function ($a) { return $a; }, $my_array['months'][$key]));
$monthly_sales_array['months'][$key] = $sum;
}
print_r($monthly_sales_array);
OUTPUT:
Array
(
[months] => Array
(
[07-2014] => 354.5
[06-2014] => 92.01
)
)
You'll need a foreach. (there are other ways to do this, but I find this the easiest)
foreach($monthly_sales_array['months'] as &$month) {
$total=0;
foreach($month as $day) {
$total+=$day;
}
$month=$total;
}
Note: the & operator in foreach($monthly_sales_array['months'] as &$month) means that when you update $month, it will be updated in the $monthly_sales_array.
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
)
)
)
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[TotalInv] => 8
)
)
)
i want to add [TotalInv] on First array based on [reg_id] , how can i do this?? pls help me.
i want this output::
Array
(
[RX Housing] => Array
(
[0] => Array
(
[reg_id] => 63
[vRegionName] => Boston
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 0
[TotalInvamt] => 0.00
[TotalInv] => 2
)
[1] => Array
(
[reg_id] => 41
[vRegionName] => Chicago
[Forwarded] => 1
[Accepted] => 0
[Rejected] => 0
[Dupe] => 0
[PotentialLease] => 0
[Lcpc] => 0
[CloseLead] => 1
[TotalInvamt] => 954.00
[TotalInv] => 8
)
)
)
Thank in advance.
Vims Mak
I would recommend when defining your first array to set the keys to the reg_id.
For example if you were creating this array from query results:
$results = array();
while($row = mysql_fetch_assoc($blah)){
$id = $row['reg_id'];
$results[$id] = $row;
}
Then when you loop through your second array you can easily update the first.
foreach($secondArray as $key => $row){
$id = $row['reg_id'];
$val = $row['TotalInv'];
$first[$id]['TotalInv'] = $val;
}
Arrays are a beautiful thing! Hope that helps!
-fie
Do you mean:
foreach($yourArr["RX Housing"] as $key => $val) {
if($val["reg_id"] == 10) {
$yourArr["RX Housing"][$key]["TotalInv"] = 2;
}
else {
$yourArr["RX Housing"][$key]["TotalInv"] = 20;
}
}
Hope it helps
try this
$a = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'vRegionName' => 'Boston',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 0,
'TotalInvamt' => 0.00
),
Array
(
'reg_id' => 41,
'vRegionName' => 'Chicago',
'Forwarded' => 1,
'Accepted' => 0,
'Rejected' => 0,
'Dupe' => 0,
'PotentialLease' => 0,
'Lcpc' => 0,
'CloseLead' => 1,
'TotalInvamt' => 954.00
)
)
);
$b = Array
(
'RX Housing' => Array
(
Array
(
'reg_id' => 63,
'TotalInv' => 2
),
Array
(
'reg_id' => 41,
'TotalInv' => 8
)
)
);
foreach ($a['RX Housing'] as $value) {
foreach ($b['RX Housing'] as $value1) {
foreach ($value as $key => $result) {
if (array_key_exists($key, $value1)) {
if ($result == $value1[$key]) {
$value = array_merge($value, $value1);
$c['RX Housing'][] = $value;
}
}
}
}
}
var_dump($c);
out put is
array
'RX Housing' =>
array
0 =>
array
'reg_id' => int 63
'vRegionName' => string 'Boston' (length=6)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 0
'TotalInvamt' => float 0
'TotalInv' => int 2
1 =>
array
'reg_id' => int 41
'vRegionName' => string 'Chicago' (length=7)
'Forwarded' => int 1
'Accepted' => int 0
'Rejected' => int 0
'Dupe' => int 0
'PotentialLease' => int 0
'Lcpc' => int 0
'CloseLead' => int 1
'TotalInvamt' => float 954
'TotalInv' => int 8