I have one large multidimensional array(I dnt the depth of array) and I want to display it in tabular form . this is one array from my array
[Kai Roger Tester] => Array
(
[Ikke navngitt] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 3
[waiting_approval_documents] => 1
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[Finnfjord] => Array
(
[NVD test] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 5
)
)
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 2
)
)
[Endringslogg] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[Laste opp doc] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 6
[waiting_approval_documents] => 3
[waiting_verfication_documents] => 4
[under_construction_documents] => 13
)
)
[Prosess 1] => Array
(
[AF Decom] => Array
(
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[status] => Array
(
[documents_green] => 7
[documents_yellow] => 0
[documents_red] => 2
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 11
)
)
Folder Name green yellow red
Kai Roger Tester 1 0 6
Ikke navngitt' 0 0 3
Finnfjord 0 0 0
NVD test 0 0 1
Process1 1 1 0
I tired with below method
public static function getfoldertable($array, $prefix = '') {
$body_start = "<tbody>";
if (count($array) > 0 && is_array($array)) {
$i = 0;
foreach ($array as $key => $row) {
$body_start.='<tr>
<td>'.$i.'</td>
<td>'.$key.'</td>
<td>'.$row['status']['documents_green'].'</td>
<td>'.$row['status']['documents_yellow'].'</td>
<td>'.$row['status']['documents_red'].'</td>
</tr>';
$body_start.= self::getfoldertable($row, $prefix . '-');
$i++;
}
$body_start.="</tbody>";
}
//echo $body_start; die;
return $body_start;
}
Can anyone help me how can i display this?
Thanks in advance
Here is a function you could use for generating that output:
function getTreeHTML($tree, $level = 0) {
$html = "";
$indent = str_repeat(" ", $level * 2);
foreach ($tree as $key => $value) {
if ($key == "status") continue;
$html .= "
<tr><td>$indent$key</td>
<td>{$value['status']['documents_green']}</td>
<td>{$value['status']['documents_yellow']}</td>
<td>{$value['status']['documents_red']}</td>
</tr>" . getTreeHTML($value, $level+1);
}
return $html;
}
If the input data is defined like this:
$input = array (
'Kai Roger Tester' =>
array (
'Ikke navngitt' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 3,
'waiting_approval_documents' => 1,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'Finnfjord' =>
array (
'NVD test' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 5,
),
),
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 2,
),
),
'Endringslogg' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'Laste opp doc' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 6,
'waiting_approval_documents' => 3,
'waiting_verfication_documents' => 4,
'under_construction_documents' => 13,
),
),
'Prosess 1' =>
array (
'AF Decom' =>
array (
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'status' =>
array (
'documents_green' => 7,
'documents_yellow' => 0,
'documents_red' => 2,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 11,
),
),
);
...and you would call that function like this:
$html = getTreeHTML($input);
...embedding that HTML result as follows in a table:
<table border=1>
<tr><th>Folder Name</th><th>green</th><th>yellow</th><th>red</th></tr>
<?=$html?>
</table>
...then the output would look in a browser like this:
Use RecursiveIterator.
Working code:
$array = array();
$array[0] = array('test' => array('asdf', 'asdf', array('asf', array('asdfads', 'asdf' => array(234,234,234)))));
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $item) {
if (is_array($item)) {
echo '<pre>';print_r($item);echo '</pre>';
}
}
Related
I have a serialized array in my database that I have extracted and unserialized. It has a structure like this:
array (
526744 =>
array (
'completed' => 13,
'total' => 24,
'topics' =>
array (
),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
)
The first number is a course id (there are actually 3 courses I've just included the first here).
I have pulled this out of the database for several different users, so I have the above several times over.
I am trying to count the lessons, so that I know how many users have passed each lesson.
I have the following code:
foreach($results as $result) {
$course_progress = unserialize($result->course_progress);
$lesson_progress = $course_progress[$course_id][lessons];
print_r(array_count_values($lesson_progress));
}
This outputs this:
Array ( [1] => 24 )
Array ( [1] => 11 [0] => 13 )
Array ( [1] => 13 [0] => 11 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 23 [0] => 1 )
Array ( [1] => 24 )
Array ( [1] => 21 [0] => 3 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [0] => 21 [1] => 3 )
This is obviously completely wrong. But I cannot think of how to get it to work. I think the issue is that I have a separate array for each user with my current code maybe?
Any help would be greatly appreciated.
Attempt 4:
$users = array(
'john' => array(
526744 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
526745 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 1,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
),
'joe' => array(
526744 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
526745 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 1,
526753 => 1,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
)
);
$counts = [];
foreach($users as $userId => $userCourses){
foreach($userCourses as $courseId => $course){
foreach($course['lessons'] as $lessonId => $lesson){
$counts[$courseId][$lessonId] = empty($counts[$courseId][$lessonId]) ? $lesson : $counts[$courseId][$lessonId]+$lesson;
}
}
}
var_dump($counts);
Obviously My array keys (john and joe) are just for testing purposes so the outermost foreach is negligible.
Returns:
array (size=2)
526744 =>
array (size=9)
526745 => int 2
526747 => int 2
526749 => int 2
526751 => int 0
526753 => int 0
526755 => int 0
526757 => int 0
526759 => int 0
526761 => int 2
526745 =>
array (size=9)
526745 => int 2
526747 => int 2
526749 => int 2
526751 => int 1
526753 => int 2
526755 => int 0
526757 => int 0
526759 => int 0
526761 => int 2
I need to append this array in php script
.Kindly help to do it perfectly .I tried multiple solution but none resulted to correct solution.
Array(
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
)
to the following array
Array(
[Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
//add the given array here
))
So that the result is
Array([Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
//added here
))
Please use least loops as much as possible.So as to get time feasible solution.Use php array function if possible.
Use foreach to add into the oti index:
<?php
$firstArr = Array(
"Don" => Array
(
"Ab" => 1,
"Ang" => 1,
"Ant" => 2,
"Bo" => 1,
"Ch" => 1,
"Chri" => 2,
"Chri" => 4,
"Deau" => 1,
"Der" => 1,
"Sylveste" => 1,
),
"Lam" => Array
(
"Ab" => 2,
"Ch" => 22,
"Dona" => 1,
"Irw" => 1,
"Kou" => 1,
"Llo" => 1,
"Ro" => 1,
"Shumy" => 1,
),
"Oti" => Array
(
"Ab" => 1,
"Arla" => 1,
"Kour" => 1,
"Osh" => 1,
"Roy " => 1,
"Tim" => 1,
"War" => 1,
//add the given array here
));
$secondArr = Array(
"Donn" => 0,
"Lamo" => 0,
"Otis" => 0,
"Stev" => 0,
"Matt" => 0,
"Samm" => 0,
"Andr" => 0,
"Jerr" => 0,
"Simm" => 0,
"Steph" => 0,
"Fredd" => 0,
"Willi" => 0,
);
foreach ($secondArr as $key => $value) {
$firstArr["Oti"][$key] = $value;
}
print_r($firstArr);
Demo
You can also use array_merge as #Mark Baker said in comments:
$firstArr["Oti"] = array_merge($firstArr["Oti"], $secondArr);
This should solve it, you use the array_merge function to merge the 2 arrays :)
$secondArray['Oti'] = array_merge($secondArray['Oti'], $firstArray);
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/>";
?>
This is the array I am using (3 tiers), and I am trying to add the number of sales (nosales) from the first month in the first array to the first month in the second array (Should be 150) and so on through all the variables (months shouldn't be added) so I am left with a two level array with the nosales, salevalue,salecost and saleprofit totals for each month.
Array (
[0] => Array
(
[1] => Array
(
[month] => 1
[nosales] => 100
[salevalue] => 1200
[salecost] => 360
[saleprofit] => 840
)
[2] => Array
(
[month] => 2
[nosales] => 110
[salevalue] => 1320
[salecost] => 396
[saleprofit] => 924
)
)
[1] => Array
(
[1] => Array
(
[month] => 1
[nosales] => 50
[salevalue] => 350
[salecost] => 70
[saleprofit] => 280
)
[2] => Array
(
[month] => 2
[nosales] => 55
[salevalue] => 385
[salecost] => 77
[saleprofit] => 308
)
)
)
Now, I have tried looping through them to get them to add together but I am getting a number of errors. Could someone please help?
Here is the script I am using at the moment:
$acc = array_shift($results_array);
foreach ($results_array as $val) {
foreach ($val as $v) {
foreach ($v as $key => $v){
$acc[$key] += $v;
}
}
}
Thanks for your help in advance!
Is this what you wanted to do?
$aResultsArray = array(
0 => array(
1 => array(
'month' => 1,
'nosales' => 100,
'salevalue' => 1200,
'saleconst' => 360,
'saleprofit' => 840,
),
2 => array(
'month' => 2,
'nosales' => 110,
'salevalue' => 1320,
'saleconst' => 396,
'saleprofit' => 924,
),
),
1 => array(
1 => array(
'month' => 1,
'nosales' => 50,
'salevalue' => 350,
'saleconst' => 70,
'saleprofit' => 280,
),
2 => array(
'month' => 2,
'nosales' => 55,
'salevalue' => 385,
'saleconst' => 77,
'saleprofit' => 308,
),
),
);
$aSum = array();
foreach ($aResultsArray as $mYear => $aMonths) {
foreach ($aMonths as $mMonth => $aMonth) {
if (!isset($aSum[$aMonth['month']])) {
$aSum[$aMonth['month']] = array(
'month' => $aMonth['month'],
'nosales' => 0,
'salevalue' => 0,
'saleconst' => 0,
'saleprofit' => 0,
);
}
$aSum[$aMonth['month']]['nosales'] += $aMonth['nosales'];
$aSum[$aMonth['month']]['salevalue'] += $aMonth['salevalue'];
$aSum[$aMonth['month']]['saleconst'] += $aMonth['saleconst'];
$aSum[$aMonth['month']]['saleprofit'] += $aMonth['saleprofit'];
}
}
var_dump($aSum);
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