combine or intersect two arrays different length in php - php

First array is $niz:
Array (
[Swansea] => 4
[Stoke City] => 3
[Sunderland] => 3
[Southampton] => 5
[Liverpool] => 3
[Manchester United] => 2
[Hull City] => 1
[Tottenham] => 2
[Newcastle Utd] => 1
[Aston Villa] => 1
[West Ham] => 2
[Crystal Palace] => 3
[Chelsea] => 3
)
Second array is $niz1:
Array (
[Stoke City] => 2
[Sunderland] => 2
[Liverpool] => 1
[Hull City] => 1
[Tottenham] => 1
[Manchester United] => 1
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 1
)
How to combine these arrays to get $niz2 (keys are ordered like array $niz1 and values are from matching array $niz) like:
$niz2:
Array (
[Stoke City] => 3
[Sunderland] => 3
[Liverpool] => 3
[Hull City] => 1
[Tottenham] => 2
[Manchester United] => 2
[Newcastle Utd] => 1
[Crystal Palace] => 3
[Chelsea] => 3
)
I tried with function array_merge() but I get empty values and I tried with array_intersect_key().

try this
foreach ($niz1 as $k=>$n)
{
if(in_array($k,$niz1))
{
$niz2[$k]=$niz[$k];
}
}
print_r($niz2);

Try this:
$temp = array_intersect_key($niz, $niz1);
foreach ($niz1 as $k => $v) {
$niz2[$k] = $temp[$k];
}

//(overwrites the values of $niz1 with those of $niz2)
$bif=array_merge($niz1,$niz);
//(removes everything from $bif that is not in $niz1)
$result=array_intersect($niz1,$bif);

Related

PHP array How do i get only value one

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 want new array from existing array with key as repeated value from old array and value as repeated count?

My old array
array:7 [
0 => "22-Feb-2017"
1 => "22-Feb-2017"
2 => "22-Feb-2017"
3 => "27-May-2015"
4 => "10-May-2015"
5 => "10-May-2015"
6 => "08-May-2015"
]
I want new array as
array:7 [
22-Feb-2017 => "3"
27-May-2015 => "1"
10-May-2015 => "2"
08-May-2015 => "1"
]
Just try using array_count_values.The array_count_values() function counts all the values of an array.
<?php
$dates = [
0 => "22-Feb-2017",
1 => "22-Feb-2017",
2 => "22-Feb-2017",
3 => "27-May-2015",
4 => "10-May-2015",
5 => "10-May-2015",
6 => "08-May-2015",
];
$final_array = array_count_values($dates);
print_r($final_array);
OUTPUT
Array
(
[22-Feb-2017] => 3
[27-May-2015] => 1
[10-May-2015] => 2
[08-May-2015] => 1
)
Not sure if you're looking for something like this
http://php.net/manual/en/function.array-count-values.php
Please check the below output :
$a=array(0 => "22-Feb-2017",
1 => "22-Feb-2017",
2 => "22-Feb-2017",
3 => "27-May-2015",
4 => "10-May-2015",
5 => "10-May-2015",
6 => "08-May-2015");
///array_count_values counts the same values count////
$new_array = array_count_values($a);
/// now let's interchange the keys and values////
foreach($new_array as $key=>$value){
$out[$value] = $key;
}
print_r($out);
One way for going about it:
$dates = [
0 => "22-Feb-2017",
1 => "22-Feb-2017",
2 => "22-Feb-2017",
3 => "27-May-2015",
4 => "10-May-2015",
5 => "10-May-2015",
6 => "08-May-2015",
];
$result = [];
foreach ($dates as $date) {
$result[$date] = array_key_exists($date, $result) ? $result[$date] + 1 : 1;
}
print_r($result);
This is what you need exactly:
<?php
$old_array=array(
0 => "22-Feb-2017",
1 => "22-Feb-2017",
2 => "22-Feb-2017",
3 => "27-May-2015",
4 => "10-May-2015",
5 => "10-May-2015",
6 => "08-May-2015");
/*
Counting the occurances of value & storing it in a new array in the format:
array("22-Feb-2017"=>3, "27-May-2015"=>2, .........)
*/
$new_array = array_count_values($a);
print_r($new_array);
echo $new_array["22-Feb-2017"];
?>
$data=[0 => "22-Feb-2017",1 => "22-Feb-2017",2 => "22-Feb-2017",3 => "27-May-2015",4 => "10-May-2015",5 => "10-May-2015",6 => "08-May-2015"];
$a=array_count_values($data);
var_dump($a);

Sort PHP array by numerical values

I would like to sort the following names
Array ( [Jessie] => 2 [Sarah] => 3 [Simon] => 2 [John] => 2 [Kevin] => 1 [Canvasser] => 8 [canvasser] => 11 )
based on the values corresponding to them
I printed the names through the following function
// get canvasser individual names and count houses canvassed
foreach ($canvassers as $key => $value) {
// Add to the current group count if it exists
if ( isset( $canvasser_counts[$value] ) ) {
$canvasser_counts[$value]++;
}
// or initialize to 1 if it doesn't exist
else {
$canvasser_counts[$value] = 1;
}
}
print_r($canvasser_counts);
where $canvassers simply held all the names eg.
$canvassers = array('Jessie', 'Simon', 'Jessie')
Any help would be really appreciated, I have spent so long on this but can't get my head straight to sort the array correctly.
You want to use asort() - http://php.net/manual/en/function.asort.php - to sort the values in ascending order, or arsort() - http://php.net/manual/en/function.arsort.php - to sort in descending order.
Given this PHP:
$vals = array("Jessie" => 2, "Sara" => 3, "Simon" => 2, "John" => 2, "Kevin" => 1, "Canvasser" => 8, "canvasser" => 11 );
print_r($vals); // current order
asort($vals); // sort array
print_r($vals); // new order
You will get the following output:
Array
(
[Jessie] => 2
[Sara] => 3
[Simon] => 2
[John] => 2
[Kevin] => 1
[Canvasser] => 8
[canvasser] => 11
)
Array
(
[Kevin] => 1
[Jessie] => 2
[John] => 2
[Simon] => 2
[Sara] => 3
[Canvasser] => 8
[canvasser] => 11
)

Sum keys of arrays if they match and order it

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
…

PHP split array based on search results

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.

Categories