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.
Related
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);
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);
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 );
}
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
…