There is something I want to ask to take the array value based on the biggest score in score keys and the smallest strlen in word keys, this sample array:
<?php
$data = array(
'0' => array('score' => '4','word' => 'titiek'),
'1' => array('score' => '4','word' => 'titik'),
'2' => array('score' => '4','word' => 'titie'),
'3' => array('score' => '3','word' => 'tuatuka'),
'4' => array('score' => '3','word' => 'titiks'),
);
$result = //do something??
print_r($result);
?>
let we see in $data array, condition the biggest score and the smallest strlen we have $data[1] and $data[2], right? but, I need the first queue. the result print_r($result) is a:
Array ( [score] => 4 [word] => titik )
You can use usort to sort your array by score and then word length, and then your desired result will be in $data[0]. Note that we rank equal word lengths higher so that we return the first one of the shortest length that we see in the array.
usort($data, function($a, $b) {
if ($a['score'] == $b['score'])
return (strlen($a['word']) >= strlen($b['word'])) ? 1 : -1;
else
return $b['score'] - $a['score'];
});
print_r($data[0]);
An alternate method (not relying on usort behaviour) is to find the maximum score and then process the array looking for the first, shortest string with that score:
$max_score = max(array_column($data, 'score'));
$minlength = PHP_INT_MAX;
foreach ($data as $key => $value) {
if ($value['score'] != $max_score) continue;
if (strlen($value['word']) < $minlength) {
$minlength = strlen($value['word']);
$index = $key;
}
}
print_r($data[$index]);
Output (same for both):
Array (
[score] => 4
[word] => titik
)
Demo on 3v4l.org
You can use array_reduce and by using ">=" "<=" it will keep the previous value
$result = array_reduce($data, function ($previous, $current) {
return $previous['score'] >= $current['score'] && strlen($previous['word']) <= strlen($current['word']) ? $previous : $current;
});
var_dump($result);
Here is how you can find it:
$data = array(
'0' => array('score' => '4','word' => 'titiek'),
'1' => array('score' => '4','word' => 'titik'),
'2' => array('score' => '4','word' => 'titie'),
'3' => array('score' => '3','word' => 'tuatuka'),
'4' => array('score' => '3','word' => 'titiks'),
);
$maxScore = 0; $minLen = 0;
foreach($data as $key => $arr) {
$score = $arr['score'];
$len = strlen($arr['word']);
if($score > $maxScore) {
$maxKey = $key;
$minLen = $len;
$maxScore = $score;
} else if ($score == $maxScore && $len < $minLen) {
$maxKey = $key;
$minLen = $len;
}
}
var_dump($data[$maxKey]);
I have used var_dump to show the result. $data[$maxKey] is the required result.
Related
I have following code that removes adjacent duplicates from the $myArray
<?php
$myArray = array(
0 => 0,
1 => 0,
2 => 1,
5 => 1,
6 => 2,
7 => 2,
8 => 2,
9 => 0,
10 => 0,
);
$previtem= NULL;
$newArray = array_filter(
$myArray,
function ($currentItem) use (&$previtem) {
$p = $previtem;
$previtem= $currentItem;
return $currentItem!== $p ;
}
);
echo "<pre>";
print_r($newArray);
?>
It works perfectly fine, but I have to change a condition bit for value 2. That means for other values we can pick first occurrence and ignore the others. But for 2, we need to pick last occurrence and ignore others.
So required output is
Array
(
[0] => 0 //first occurrence of 0 in $myArray
[2] => 1 //first occurrence of 1 in $myArray
[8] => 2 //last occurrence of 2 in the $myArray
[9] => 0 //first occurrence of 0 in $myArray
)
How to modify my code to achieve above result??
In reality I have multidimensional array, but for better explanation I have used single dimensional array here in the question.
UPDATE
My actual array is
$myArray = array(
0 => array("Value"=>0, "Tax" => "11.00"),
1 => array("Value"=>0, "Tax" => "12.00"),
2 => array("Value"=>1, "Tax" => "13.00"),
5 => array("Value"=>1, "Tax" => "14.00"),
6 => array("Value"=>2, "Tax" => "15.00"),
7 => array("Value"=>2, "Tax" => "16.00"),
8 => array("Value"=>2, "Tax" => "17.00"),
9 => array("Value"=>0, "Tax" => "18.00"),
10 => array("Value"=>0, "Tax" => "19.00"),
);
And my actual code
$previtem= NULL;
$newArray = array_filter(
$myArray,
function ($currentItem) use (&$previtem) {
$p["Value"] = $previtem["Value"];
$previtem["Value"] = $currentItem["Value"];
return $currentItem["Value"]!== $p["Value"] ;
}
);
Thanks
This should do what you are looking for.
function array_filter($a) {
$na = array();
$first = true;
$p = null;
$wantlast = false;
foreach ($a as $v) {
if ($wantlast) {
($v != $p) ? $na[] = $p: null;
}
$wantlast = ($v == 2) ? true : false;
if (!$wantlast) {
(($v != $p) || ($first))? $na[] = $v : null;
}
$p = $v;
$first = false;
}
return $na;
}
$myArray = array(
0 => 0,
1 => 0,
2 => 1,
5 => 1,
6 => 2,
7 => 2,
8 => 2,
9 => 0,
10 => 0,
);
$previtem= NULL;
$newArray = array_filter(
$myArray,
function ($currentItem, $key) use (&$previtem,$myArray) {
$p = $previtem;
if($currentItem != 2){
$previtem = $currentItem;
}else{
$lastkey = array_search(2,(array_reverse($myArray, true)));
if($key != $lastkey)
$currentItem = $previtem;
}
return $currentItem!== $p ;
}, ARRAY_FILTER_USE_BOTH
);
echo "<pre>";
print_r($newArray);
Having an array similar to the one below:
$steps = array(0 => 'aaa', 1 => 'bbb', 2 => 'ccc', ......, 7 => 'hhh', 8 => 'iii', .....);
How can I calculate how many steps (key) do I need to reach key 2 from key 7 respecting the sequence?
If you have numeric keys that never have any missing numbers, you can use basic subtraction.
If you need to account for possible missing numbers, or the keys are not numeric, you can use a combination of array_keys() and array_search():
$array = array(
0 => 'aaa',
1 => 'bbb',
3 => 'ccc',
'four' => 'ddd',
900 => 'eee',
13 => 'fff'
);
$from = 1;
$to = 900;
$keys = array_keys($array);
$from_index = array_search($from, $keys); // 1
$to_index = array_search($to, $keys); // 4
$steps = $to_index - $from_index;
// 3 steps: from 1-3, from 3-'four' and from 'four'-900
I solved this problem by writing this code:
$tot_step = 0;
$steps = array(
0 => 'aaa',
1 => 'bbb',
2 => 'ccc',
3 => 'ddd',
4 => 'eee',
5 => 'fff',
6 => 'ggg',
7 => 'hhh',
8 => 'iii',
9 => 'jjj',
10 => 'aaa'
);
$from = "ddd";
$to = "bbb";
$from_index = array_search($from, $steps);
$to_index = array_search($to, $steps);
$last = $steps[(count($steps)-1)];
if ($from_index > 0) {
if ($to == $last)
$to_index = (count($steps)-1);
$arr_l = count($steps);
$mila = array();
for ($ind = $from_index; $ind <= ($arr_l-1); $ind++) {
if ($to == $last) {
if ($steps[$ind] != $last)
$mila[] = $steps[$ind];
} else {
$mila[] = $steps[$ind];
}
unset($steps[$ind]);
}
if (!empty($mila)) {
for ($i = (count($mila)-1); $i >= 0; $i--)
array_unshift($steps, $mila[$i]);
}
$to_new = array_search($to, $steps);
foreach ($steps as $key => $value) {
if ($key == $to_new)
break;
else
$tot_step++;
}
} elseif ($from_index == 0) {
if ($to_index == $from_index) {
$tot_step = (count($steps)-1);
} else {
foreach ($steps as $key => $value) {
if ($key == $to_index)
break;
else
$tot_step++;
}
}
}
echo $tot_step;
I hope it will be useful to someone
I need to find the last found element of a specific value from an array. I giving an example in php of what I'm actually seeking for.
$Data = array(
'0' => 'car',
'1' => 'bike',
'2' => 'bus',
'3' => 'bike',
'4' => 'boat'
);
$key = array_search('bike', $Data) // it returns $key = 1 as result which the first element matched inside the array.
I want $key = 3 which is the last matched element.
Any suggestion appreciated.
PHP code demo
<?php
ini_set("display_errors", 1);
$Data = array(
'0' => 'car',
'1' => 'bike',
'2' => 'bus',
'3' => 'bike',
'4' => 'boat'
);
$toSearch="bike";
$index=null;
while($key=array_search($toSearch, $Data))
{
$index=$key;
unset($Data[$key]);
}
echo $index;
Here is the more simple and highly performace way. For it only calculate once, you can access it many time. The live demo.
$data = array_flip($Data);
echo $data['bike'];
after the flip, only keep the last element of the same elements. Here is the print_r($data)
Array
(
[car] => 0
[bike] => 3
[bus] => 2
[boat] => 4
)
We can use array_reverse to reverse array.
$key = array_search('bike', array_reverse($Data,true));
It will return 3.
you can use krsort to sort the array by key.
krsort($Data);
$key = array_search('bike', $Data);
echo $key;
Working example: https://3v4l.org/fYOgN
For this I am created one function it is very easy to use. You can pass only array and parameters.
function text_to_id($value, $arr_master) {
$id_selected = 0;
$search_array = $arr_master;
if (in_array($value, $search_array)) {
$id_selected = array_search($value, $search_array);
// pr($id_selected);exit;
}
if (!$id_selected) {
foreach ($search_array as $f_key => $f_value) {
if (is_array($f_value)) {
if (in_array($value, $f_value)) {
$id_selected = $f_key;
break;
}
} else if ($value == $f_value) {
$id_selected = $f_key;
break;
}
else;
}
}
return $id_selected;
}
this function use like this
$variable = text_to_id('bike', $your_array);
This question already has answers here:
Sort array using array_multisort() with dynamic number of arguments/parameters/rules/data
(5 answers)
Closed 2 years ago.
I have the problem with sort direction. I try to sort multi-dimensional array with direction. I can't use array_multisort() directly, because I don't know how many parametrs will be. I use call_user_func_array('array_multisort', $params); And it works, but I can't set sort direction (SORT_ASC,SORT_DESC). How can I set sort direction for call_user_func_array('array_multisort', $params);?
Here is my code, you can try it
function get_fields($data, $order_by) {
$order_row = preg_split("/[\s,]+/", $order_by);
for ($i=0;$i<count($order_row);$i++) {
foreach ($data as $key => $row) {
$tmp[$i][$key] = $row[$order_row[$i]];
}
}
return $tmp;
}
function ordering($data, $order_by) {
$tmp = get_fields($data, $order_by);
$params = array();
foreach($tmp as &$t){
$params[] = &$t;
}
$params[1] = array("SORT_DESC","SORT_DESC","SORT_DESC","SORT_DESC"); // like that no warning but no sorting
$params[] = &$data;
call_user_func_array('array_multisort', $params);
return array_pop($params);
}
$data = array (
array('id' => 1,'name' => 'Barack','city' => 9),
array('id' => 7,'name' => 'boris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2)
);
$order_by = "city desc, name";
echo "<br>ORDER BY $order_by<br>";
$ordered = ordering($data, $order_by);
echo "<pre>";
var_dump($ordered);
echo "</pre>";
I want to do a sort like MySQL ORDER BY city DESC, name. It's my goal.
To be able to sort an array multiple times and achieve a result like ORDER BY city DESC, name ASC you need a function that does a stable sort.
As far as I know PHP doesn't have one so you have to sort it once with a comparator function like this
$data = array (
array('id' => 3,'name' => 'coris','city' => 2),
array('id' => 1,'name' => 'Barack','city' => 9),
array('id' => 7,'name' => 'boris','city' => 2),
array('id' => 3,'name' => 'coris','city' => 2),
);
$order_by = array(
'city' => array('dir' => SORT_DESC, 'type' => SORT_NUMERIC),
'name' => array('dir' => SORT_ASC, 'type' => SORT_STRING),
);
function compare($row1,$row2) {
/* this function should determine which row is greater based on all of the criteria
and return a negative number when $row1 < $row2
a positive number when $row1 > $row2
0 when $row1 == $row2
*/
global $order_by;
foreach($order_by as $field => $sort) {
if($sort['type'] != SORT_NUMERIC) {
// strings are compared case insensitive and assumed to be in the mb_internal_encoding
$cmp = strcmp(mb_strtolower($row1[$field]), mb_strtolower($row2[$field]));
} else {
$cmp = doubleval($row1[$field]) - doubleval($row2[$field]);
}
if($sort['dir'] != SORT_ASC) $cmp = -$cmp;
if($cmp != 0) return $cmp;
}
return 0;
}
usort($data,'compare');
I had the same problem. It seems that call_user_func_array() can't handle the constants.
I've solved this problem by dynamically building an argument string and evaluating this string:
$args = array($arr1, $arr2);
$order = array(SORT_ASC, SORT_DESC);
$evalstring = '';
foreach($args as $i=>$arg){
if($evalstring == ''){ $evalstring.= ', '; }
$evalstring.= '$arg';
$evalstring.= ', '.$order[$i];
}
eval("array_multisort($evalstring);");
I know eval() is evil and this is not a clean way, but it works ;-)
It is working for me :
$arrayThatNeedToSort = array('data..');
$multiSortprop = array(['data.....']=> SORT_DESC,['data.....'] => SORT_ASC)
$properties = array();
foreach ($multiSortprop as $sortArr => $sortArg) {
array_push($properties,$sortArr);
array_push($properties,$sortArg);
}
array_push($properties,$arrayThatNeedToSort);
array_multisort(...$properties);
var_dump(end($properties));
I've got an array that uses UNIX timestamps for array keys. The array will typically hold data for anywhere from 15 minutes to maybe an hours worth of time, however there are only entries for seconds that have data.
Most of the data will be spread out, there will be occasional spans of data for consecutive seconds though. What I'd like to do, is retrieve the first and last second of the longest consecutive span of seconds in the array.
If I have this array
Array
(
[1276033307] => 119.0
[1276033331] => 281.8
[1276033425] => 28.2
[1276033431] => 88.2
[1276033432] => 196.2
[1276034207] => 205.5
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
[1276034230] => 79.8
)
I would either need the keys 1276034226 and 1276034228, or the following array returned.
Array
(
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
EDIT:
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$finalArray = array();
foreach($array as $k => $v){
$tempArrays = array();
$index = 0;
while(isset($array[$k + $index])){
$tempArrays[$k+$index] = $array[$k+$index++];
}
if(count($tempArrays) > count($finalArray))
$finalArray = $tempArrays;
}
print_r($finalArray);
the output is the same...
ORIGINAL:
Note: Only the first occurrence of the longest span will be recorded.
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$longspan = 0;
foreach($array as $k => $v){
$index = 1;
while(isset($array[$k+$index])){
$index++;
}
$curspan = --$index;
if($curspan > $longspan){
$longspan = $curspan;
$start = $k;
}
}
for($i=0; $i <= $longspan; $i++)
$results[$start + $i] = $array[$start + $i];
print_r($results);
Outputs:
Array (
[1276034226] => 73.8
[1276034227] => 75.8
[1276034228] => 77.8
)
This code does it in a single loop (i.e. is a Greedy algorithm):
$array = array(
1276033307 => 119.0,
1276033331 => 281.8,
1276033425 => 28.2,
1276033431 => 88.2,
1276033432 => 196.2,
1276034207 => 205.5,
1276034226 => 73.8,
1276034227 => 75.8,
1276034228 => 77.8,
1276034230 => 79.8,
);
$long_arr = array();
$curr_arr = array();
$last_key = -1;
foreach($array as $k => $v) {
if ($k != $last_key + 1) {
$curr_arr = array();
}
$curr_arr[$k] = $v;
if (count($curr_arr) > count($long_arr)) {
$long_arr = $curr_arr;
}
$last_key = $k;
}
print_r($long_arr);