We have three array something like this but these are dynamic
Array
(
[04/07/2013] => 2
[05/02/2013] => 1
[06/02/2013] => 1
[08/07/2013] => 2
[08/08/2013] => 3
[09/07/2013] => 2
[11/07/2013] => 1
[16/03/2013] => 1
[17/07/2013] => 1
[18/04/2013] => 2
[18/07/2013] => 1
[21/05/2013] => 2
[24/05/2013] => 8
[25/04/2013] => 2
[26/04/2013] => 1
[26/06/2013] => 1
[30/05/2013] => 1
)
Array
(
[01/08/2013] => 42
[02/08/2013] => 2
[03/08/2013] => 3
[07/08/2013] => 29
[09/08/2013] => 4
[10/08/2013] => 4
[11/08/2013] => 31
[19/07/2013] => 4
[20/07/2013] => 4
[22/07/2013] => 13
[23/07/2013] => 69
[29/07/2013] => 4
[31/07/2013] => 5
)
Array
(
[13/02/2013] => 2
[26/04/2013] => 2
[04/06/2013] => 2
[20/06/2013] => 2
[04/07/2013] => 2
[09/07/2013] => 1
[01/08/2013] => 1
[07/08/2013] => 1
[08/08/2013] => 3
)
We want to combine into other array with keys(key must be remain same).If all three array keys has same put it into the same key other wise create a key for and put it value if array has key other wise put it with zero value.
Here we tried for this
$maximum = max($countVisi,$countClic,$countClai);
if($countClic==$maximum){
$maxim = $clickArray;
}elseif($countVisi>=$maximum){
$maxim = $visitArray;
}elseif($countClai>=$maximum){
$maxim = $claimsArray;
}else{
$maxim = $visitArray;
}
we count the maximum index array and foreach the loop like this
foreach($maxim as $key=>$values){
if($visitArray[$key]){
$vv[$key] = $visitArray[$key];
}else{
$vv[$key] = 0;
}
if($clickArray[$key]){
$cc[$key] = $clickArray[$key];
}else{
$cc[$key] = 0;
}
if($claimsArray[$key]){
$kk[$key] = $claimsArray[$key];
}else{
$kk[$key] = 0;
}
$combineArrayNext[$key][] = $vv[$key];
$combineArrayNext[$key][] = $cc[$key];
$combineArrayNext[$key][] = $kk[$key];
//$vvvvv = explode('/' , $key);
//$myKey[$key] = "'".date('d M Y' , strtotime($vvvvv[2]."/".$vvvvv[1]."/".$vvvvv[0]))."'";
}
The problem is that we are getting only according to max array key.It leave the key which are not exits in max array.
Sorry for the less explaining , I think you understand my problem.Please share some idea with us to solve my problem.
Thanks
Hope this code helps you:
$dates = array_merge(array_keys($arr1), array_keys($arr2), array_keys($arr3));
foreach($dates as $date) {
$new_arr[$date] = array('click' => isset($arr1[$date])? $arr1[$date] : 0,
'visit' => isset($arr2[$date])? $arr2[$date] : 0,
'claim' => isset($arr3[$date])? $arr3[$date] : 0 );
}
Related
I have problem in multidimensional array
I have a PHP array as follows:
$array1 = array(
1 => '01-Jul-2017',
2 => '02-Jul-2017',
3 => '03-Jul-2017',
4 => '04-Jul-2017',
5 => '05-Jul-2017',
...,
31 => '31-Jul-2017',);
$array2 = array(
1 => '01-Jul-2017',
3 => '03-Jul-2017',
4 => '04-Jul-2017',
5 => '05-Jul-2017',
6 => '06-Jul-2017',
...,
30 => '31-Jul-2017');
foreach($array1 as $array_one) {
foreach($array2 as $array_two) {
if($array_one == $array_two) {
echo 'write';
} else {
**I want to display that does not exist in $ array2 output 02-Jul-2017;**
}
}
}
How do i get just the value 02-Jul-2017
You can use array_diff() inbuilt function.
$array1 = array(
1 => '01-Jul-2017',
2 => '02-Jul-2017',
3 => '03-Jul-2017',
4 => '04-Jul-2017',
5 => '05-Jul-2017');
$array2 = array(
1 => '01-Jul-2017',
3 => '03-Jul-2017',
4 => '04-Jul-2017',
5 => '05-Jul-2017',
6 => '06-Jul-2017');
$result=array_diff($array1,$array2);
print_r($result);
OUTPUT
Array ( [2] => 02-Jul-2017 )
Let me know if it not works.
I have a pretty simple array:
array(
1 => 'asdf.php',
2 => 'jkl.php',
3 => 'qwer.php',
4 => 'ty.php',
5 => 'edit.php?param=my_val_a',
6 => 'edit.php?param=my_val_g',
7 => 'edit.php?param=my_val_i',
8 => 'zxcv.php',
9 => 'oiu.php',
10 => 'edit.php?param=my_val_w',
11 => 'bnm.php',
12 => 'hgb.php',
13 => 'edit.php?param=my_val_p'
)
My goals is try to retain the same order of the array except I want all the items with the string "?param=my_val" to be next to each other, also retaining their order. So the end result would be:
array(
1 => 'asdf.php',
2 => 'jkl.php',
3 => 'qwer.php',
4 => 'ty.php',
5 => 'edit.php?param=my_val_a',
6 => 'edit.php?param=my_val_g',
7 => 'edit.php?param=my_val_i',
8 => 'edit.php?param=my_val_w',
9 => 'edit.php?param=my_val_p'
10 => 'zxcv.php',
11 => 'oiu.php',
12 => 'bnm.php',
13 => 'hgb.php',
)
I've been playing around with usort() and I've been able to get them all together, but keeping the same basic order has got me struggling. How would I go about doing this?
How about this (assuming you have your stuff in $arr):
$parts_with_param = array();
$parts_with_no_param = array();
foreach ($arr as $val) {
if (strpos($val, '?param=my_val') !== false) {
$parts_with_param[] = $val;
} else {
$parts_with_no_param[] = $val;
}
}
// merge into a new variable or overwrite $arr if you like
$new_arr = array_merge($parts_with_param, $parts_with_no_param);
I have an array with 3 arrays inside.
I have to merge it BUT if a value of any of the three arrays matchs with the value of any of the other two arrays, the keys should be sum.
I.E.
[bing][10] matchs with [google][10] (url not cleaned actually, my mistake) and [yahoo][10]
so the new merged array should have [url][30] in the first position, and so on.
how can I achieve this?
My array: http://pastebin.com/tSfrCcMJ
$array = array (
'bing' => array (
10 => 'http://stackoverflow.com/',
9 => 'http://www.stackoverflow.es/',
8 => 'http://stackoverflow.com/questions',
7 => 'http://www.stackoverflow.es/empresa/avisolegal',
6 => 'http://stackoverflow.net/',
5 => 'http://chat.stackoverflow.com/',
4 => 'http://blog.stackoverflow.com/',
3 => 'http://chat.stackoverflow.com/?tab=all&sort=active&page=16',
2 => 'http://meta.stackoverflow.com/',
1 => 'http://careers.stackoverflow.com/ewernli'
),
'google' => array (
10 => 'http://stackoverflow.com/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CBYQFjAA&usg=AFQjCNFOHjfhg0MrXOGxhxoLkWY6BP7Erw',
9 => 'http://stackoverflow.com/users/login',
8 => 'http://en.wikipedia.org/wiki/Stack_overflow&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CC0QFjAH&usg=AFQjCNFaLvYDIANOTluG7kTQZppgPK1OuQ',
7 => 'http://blog.stackexchange.com/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDAQFjAI&usg=AFQjCNFM47UgedUUcCIIENkkEpGT1F5-VQ',
6 => 'http://itc.conversationsnetwork.org/series/stackoverflow.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDMQFjAJ&usg=AFQjCNEhtBxP6KPK9A2IIHzjqGETn5kVgA',
5 => 'http://stackoverflow.org/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDYQFjAK&usg=AFQjCNFsYAEUQYofh1C2k0IfppDSwwxAUA',
4 => 'http://stackoverflow.net/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDgQFjAL&usg=AFQjCNH55YZyZeh8q75--kYkyCg8nRuf4g',
3 => 'http://www.crunchbase.com/company/stack-exchange&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CDsQFjAM&usg=AFQjCNETf6XyPdfFqJC5-6F5NFxGjDY2wA',
2 => 'http://embeddedgurus.com/stack-overflow/&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEAQFjAN&usg=AFQjCNE-vRAAhmbu_OzwpI6EoI-9va12LA',
1 => 'http://www.haskell.org/haskellwiki/Stack_overflow&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEMQFjAO&usg=AFQjCNEhsp34I-FC-dW0fG0-ZogG7T-qXg',
0 => 'http://highscalability.com/blog/2011/3/3/stack-overflow-architecture-update-now-at-95-million-page-vi.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEYQFjAP&usg=AFQjCNEf7K09RvPYSDxWKKhDdCpDj1hs1w'
),
'yahoo' => array (
10 => 'http://stackoverflow.com/',
9 => 'http://en.wikipedia.org/wiki/Stack_overflow',
8 => 'http://stackoverflow.com/about',
7 => 'http://en.wikipedia.org/wiki/Stackoverflow',
6 => 'http://blog.stackoverflow.com/',
5 => 'http://facebook.stackoverflow.com/',
4 => 'http://stackoverflow.com/questions',
3 => 'http://stackoverflow.net/',
2 => 'http://stackoverflow.com/faq',
1 => 'http://stackoverflow.com/questions/ask'
)
);
Desired result (I only did the 'http://stackoverflow.com/' match, keys sum):
krsort($array);
$result = array ( 30 => 'http://stackoverflow.com/', 9 => 'http://en.wikipedia.org/wiki/Stack_overflow', 8 => 'http://stackoverflow.com/about', 7 => 'http://en.wikipedia.org/wiki/Stackoverflow', 6 => 'http://blog.stackoverflow.com/', 5 => 'http://facebook.stackoverflow.com/', 4 => 'http://stackoverflow.com/questions', 3 => 'http://stackoverflow.net/', 2 => 'http://stackoverflow.com/faq', 1 => 'http://stackoverflow.com/questions/ask', 0 => 'http://highscalability.com/blog/2011/3/3/stack-overflow-architecture-update-now-at-95-million-page-vi.html&sa=U&ei=oMg7T_rpJ4rz-gazm_SsBw&ved=0CEYQFjAP&usg=AFQjCNEf7K09RvPYSDxWKKhDdCpDj1hs1w', );
You cannot have the score for keys because you risk overwriting values when two entries have the same score. What you can do is:
$urls = array();
array_walk_recursive($array, function ($url, $score) use (&$urls) {
$key = strtok($url, '&');
$urls[$key] = isset($urls[$key]) ? $urls[$key] + $score : $score;
});
arsort($urls);
print_r($urls);
gives
Array
(
[http://stackoverflow.com/] => 30
[http://en.wikipedia.org/wiki/Stack_overflow] => 17
[http://stackoverflow.net/] => 13
[http://stackoverflow.com/questions] => 12
[http://blog.stackoverflow.com/] => 10
[http://stackoverflow.com/users/login] => 9
…
i have a (dynamic) array, which in this example contains 4 sets of data (5 fields per set), but it could be only one set or up to 25 sets.
Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
which i'd like to turn into something like
Array ( Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => ),
Array ( [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => ),
Array ( [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => ),
Array ( [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
)
the original array is created this way:
$light = Array();
foreach( $_POST as $key => $val )
{
//field names that start with light to go in this array
if (strpos($key, 'light') === 0) {
$light[$key] = $val;
}
}
the field name digit is already added with JS before form submission, and not by php script.
any hint much appreciated.
This is not an exacty answer to you question, but...
You can use arrays in POST variables like so:
<input name="light[1][wattage]" />
<input name="light[1][voltage]" />
<input name="light[2][wattage]" />
<input name="light[2][voltage]" />
will give you:
$_POST['light'] == array(
1 => array(
'wattage' => '...',
'voltage' => '...',
),
2 => array(
'wattage' => '...',
'voltage' => '...',
),
)
Try this:
$prefixes = array();
$postfixes = array();
foreach($original_array as $key=>$value)
{
preg_match('/^([^\d]*)(\d+)$/',$key,$matches);
if(count($matches)>1)
{
if(!in_array($matches[1], $prefixes)) $prefixes[] = $matches[1];
if(!in_array($matches[2], $postfixes)) $postfixes[] = $matches[2];
}
}
$new_array = array();
foreach($postfixes as $postfix)
{
$new_element = array();
foreach($prefixes as $prefix)
{
if(isset($original_array[$prefix.$postfix])) $new_element[$prefix.$postfix] = $original_array[$prefix.$postfix];
}
$new_array[] = $new_element;
}
given an $original_array equal to described, will produce $new_array:
Array
(
[0] => Array
(
[lightwattage1] => 50
[lightvoltage1] => 12
[lightquantity1] => 2
[lightusage1] => 4
[lightcomment1] =>
)
[1] => Array
(
[lightwattage2] => 60
[lightvoltage2] => 24
[lightquantity2] => 4
[lightusage2] => 5
[lightcomment2] =>
)
[2] => Array
(
[lightwattage3] => 30
[lightvoltage3] => 240
[lightquantity3] => 4
[lightusage3] => 2
[lightcomment3] =>
)
[3] => Array
(
[lightwattage4] => 25
[lightvoltage4] => 12
[lightquantity4] => 2
[lightusage4] => 6
[lightcomment4] =>
)
)
I was uncertain about how much you knew about the elements or their order, so this code basically takes any collection of elements that end in numbers and rearranges them in groups that have the same ending number.
Try this:
$outarr = array()
$subarr = array()
$i=0;
foreach($_POST as $key => $val)
{
//only include keys starting with light:
if (strpos("light", $key)==0)
{
//create a new subarray each time we find a key that starts with "lightwattage":
if ($i>0 && strpos("lightwattage", $key)==0)
{
$outarr[] = $subarr;
}
$subarr[$key] = $val;
$i++;
}
}
$outarr[] = $subarr;
//$outarr contains what you want here
I have a multidimensional array which is created by a MySQL query which collects results based on a number of groups and sums. The array is below.
I'm interested in the costtotal and hitcount for each type of 'ad_type', 'click_status' and 'link_status' variation.
The possible values of the 3 types of variable are known:
i.e.
ad_type 0 / 1
click_status 2 / 3
link_status 1 / 2
I would like to create a new array based on the results of each combination.
I'm guessing a search or split would do it but I'm not having much luck.
How would I go about doing this?
Array
(
[0.261346210037681] => Array
(
[costtotal] => 0.0015
[hitcount] => 1
[ad_type] => 0
[click_status] => 2
[link_status] => 1
)
[0.190427019438173] => Array
(
[costtotal] => 0.001
[hitcount] => 1
[ad_type] => 0
[click_status] => 3
[link_status] => 1
)
[0.563596305962276] => Array
(
[costtotal] => 0.007
[hitcount] => 5
[ad_type] => 1
[click_status] => 2
[link_status] => 1
)
[0.893211513658251] => Array
(
[costtotal] => 0
[hitcount] => 3
[ad_type] => 1
[click_status] => 2
[link_status] => 2
)
[0.209184847035617] => Array
(
[costtotal] => 0.004
[hitcount] => 2
[ad_type] => 1
[click_status] => 3
[link_status] => 1
)
[0.73545002260753] => Array
(
[costtotal] => 0
[hitcount] => 1
[ad_type] => 1
[click_status] => 3
[link_status] => 2
)
)
If I fully understand what you want, then this code should satisfy you:
function generateClickCounterInfo() {
return array(
'costTotal' => 0.0,
'hitCount' => 0
);
}
function generateLinkStatusStructure() {
return array(
1 => generateClickCounterInfo(),
2 => generateClickCounterInfo()
);
}
function generateClickStatusStructure() {
return array(
2 => generateLinkStatusStructure(),
3 => generateLinkStatusStructure()
);
}
function generateAdTypeArrayStructure() {
return array(
0 => generateClickStatusStructure(),
1 => generateClickStatusStructure()
);
}
function getClickCounterReport(array $data) {
$result = generateAdTypeArrayStructure();
foreach ($data as $key => $value) {
$adType = $value['ad_type'];
$clickStatus = $value['click_status'];
$linkStatus = $value['link_status'];
if (!isset($result[$adType])
|| !isset($result[$adType][$clickStatus])
|| !isset($result[$adType][$clickStatus][$linkStatus])) {
throw new Exception(
"Input data does not conform to expected format. " .
"ad_type = {$adType}, click_status = {$clickStatus}, link_status = ${linkStatus}"
);
}
$costTotal = $value['costtotal'];
$hitCount = $value['hitcount'];
$result[$adType][$clickStatus][$linkStatus]['costTotal'] += $costTotal;
$result[$adType][$clickStatus][$linkStatus]['hitCount'] += $hitCount;
}
return $result;
}
And than getClickCounterReport($data) (where $data is data provided by you) will produce following array: http://pastebin.ubuntu.com/607464/
P.S. Knowing disadvantages:
No OOP (but these functions will be easy to transform to methods)
Magick numbers (0, 1, 2, 3 etc)
No array splitting is necessary. Simply create the variables that will store the totals for each of the permutation you want to measure and iterate through your array. Add to appropriate variables based upon the value you observe in ad_type, click_status, and link_status.