Sort part of an array by value - php

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);

Related

How to create a new array using of another array keys?

Is it possible to create a new array from the keys of another like following?
it is a dynamic array chk_values are dynamically changed depends on condition
Array
(
[actBtn] => update
[chkCount] => 5
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3
)
and i want array like this for update database
$chckpoint = Array(
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3)
Simply process the original array and only move to the new array where the key starts with chk_
$in = ['actBtn' => 'update',
'chkCount' => 5,
'chk_1' => 2,
'chk_2' => 3,
'chk_3' => 2,
'chk_4' => 3,
'chk_5' => 3
];
foreach($in as $k=>$v){
if ( strpos($k,'chk_') !== false ){ $chckpoint[$k] = $v; }
}
print_r($chckpoint);
RESULT
Array
(
[chk_1] => 2
[chk_2] => 3
[chk_3] => 2
[chk_4] => 3
[chk_5] => 3
)
You can simply take the input array and check for all keys beginning with chk_. If the key matches, take it to the new array.
$chckpoint = [];
foreach($input as $key => $value)
{
if(substr($key, 0, 4) == 'chk_') $chkpoint[$key] = $value;
}

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.

multiple array combine into other array with key using php

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 );
}

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 turning array into multi-dimensional array

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

Categories