I would like someone to help me with this one, please.
I have this array.
foreach ($callersHistoryArray as $key => $value) {
//check for duplicates and find the highest call duration
if(in_array($value['caller'], $item, true) && $value['process'] == 0)
{
$duplicates = array(
'key' => $key,
'totalTime' => $callersHistoryArray[$key]['totalDurationTime']
);
echo print_r($duplicates, true);
if ($duplicates['totalTime'] > $max) {
$max = $duplicates['totalTime'];
$value['unique'] = 1;
}
}
I loop throw $callersHistoryArray and detect duplicates, create a new array and find the max totalTime from them and set unique value to 1.
When I go to second position in the foreach-loop, the previous one must change unique to 1. But I can't do that. I used prev($callersHistoryArray), to get last one but nothing.
I don't fully understand the question. I suspect you want to start with something like this:
<?php
$duplicates = array();
$maxTime = 0;
$unique = null;
foreach ($callersHistoryArray as $key => $value) {
//check for duplicates and find the highest call duration
if(in_array($value['caller'], $item, true) && $value['process'] == 0)
{
if ($maxTime < $value['totalDurationTime']) {
$maxTime = $value['totalDurationTime'];
$unique = $key;
}
$duplicates[] = $value;
}
}
echo print_r($duplicates, true); // array of duplicates
echo print_r($callersHistoryArray[$unique], true); // item with highest time
Bugs notwithstanding, that should give you an array of the duplicates, and also tell you which item has the maximum duration. I don't know where $item comes from, so YMMV.
You might also need to handle two items having the same max duration.
Related
It's a simple question, but puzzling me:
$myarray = array(
array(10,20),
array(299, 315),
array(156, 199)
);
How do I check if given $x , lies in between, in any of those particular individual array values? I want to search each individual entry array.
For Example, I want to search, if $x is somewhere between: 10 to 20 and then between 299 to 315 and then between 156 to 199.
Try this:
function is_in_array_range($array, $search) {
foreach ($array as $value) {
$min = min($value);
$max = max($value);
if ($search >= $min && $search <= $max) {
return true;
}
}
return false;
}
$myarray = array(
array(10,20),
array(299, 315),
array(156, 199)
);
is_in_array_range($myarray, 9); // Returns false
is_in_array_range($myarray, 11); // Returns true
The function is_in_array_range() will take two arguments. The array, and the value you want to check is in the range.
When it enters, it will loop over all elements in the array. Every time it gets the highest and lowest value of the nested array (min() and max() function), and checks if the value you are looking for is between them. If this is the case, return true (this also stops the function). If true is never reached, the value is not found, so at the end of the function, return false.
this will do it code
foreach($myarray as $value)
{
if(in_array("10", $value, true))
{
echo "Got 10";
}
}
How may i know that - In php how many times the foreach loop will get run, before that loop get executed..In other words i want to know the count of that particular loop. I want to apply some different css depends upon the count.
Use the function count to get the amount of numbers in your array.
Example:
$array = array('test1', 'test2');
echo count($array); // Echos '2'
Or if you want to be an engineer for-sorts you can set up something like so:
$array = array('test1', 'test2');
$count = 0;
foreach ($array as $a) { $count++; }
And that can count it for you, and the $count variable will hold the count, hope this helped you.
Simply count() the array and use the output as a condition, like:
if (count($array) > 100) {
// This is an array with more than 100 items, go for plan A
$class = 'large';
} else {
// This is an array with less than 100 items, go for plan B
$class = 'small';
}
foreach ($array as $key => $value) {
echo sprintf('<div id="%s" class="%s">%s</div>', $key, $class, $value);
}
Well I'm stuck with this one.
I have a foreach loop nested in another foreach loop. Now on certain conditions I need to run the outer loop once again with the same key.
While there is this function prev($array), which would set the iterator to the previous position, this does not seem to work and the php docs says
As foreach relies on the >internal array pointer, changing it within the loop may lead to unexpected behavior.
The array I am working on is an associative array, so obviously I cannot easily switch to integer indexed iterations.
$arrLocalCopy = $this->arrPrintOut;
$groupCounter = 0;
foreach($this->arrPrintOut as $kOne => $rowA){
//first and last row contain titles and totals, so we skip them
if($groupCounter < 1 || $groupCounter == sizeof($this->arrPrintOut)-1 ){ $groupCounter++; continue; }
$rowCounter = 0;
foreach($arrLocalCopy as $k => $rowB){
//skip rows that are "before" the outer loops row, as we want to compare only rows below the row we compare to.
if ($rowCounter <= $groupCounter || $rowCounter == sizeof($arrLocalCopy)-1 ) { $rowCounter++; continue; }
//If those values are the same, then values belong to the same group and must be summed together.
if($rowA['t']==$rowB['t'] && $rowA['y']==$rowB['y'] && $rowA['g']==$rowB['g'] && $rowA['q']==$rowB['q'])
{
//if values are the same, then data belongs to one group, lets group them together.
$this->arrPrintOut[$kOne]['s'] += $rowB['s'];
$this->arrPrintOut[$kOne]['b'] += $rowB['b'];
$this->arrPrintOut[$kOne]['v'] += $rowB['v'];
$this->arrPrintOut[$kOne]['r'] += $rowB['r'];
$this->arrPrintOut[$kOne]['k'] += $rowB['k'];
$this->arrPrintOut[$kOne]['n'] += $rowB['n'];
$this->arrPrintOut[$kOne]['m'] += $rowB['m'];
$this->arrPrintOut[$kOne]['l'] += $rowB['l'];
unset($this->arrPrintOut[$k]); //row has been grouped to the current row, so we remove it from the array and from the copy.
unset($arrLocalCopy[$k]);
prev($this->arrPrintOut); //we need to run the outer loop with the same key again, as probably there is another row which could be grouped together with this row.
$groupCounter--;
}
$rowCounter++;
}
$groupCounter++;
}
Here is a code sample.
$rollback is a variable to check if we prev() already
$max is the number of iterations. If you prev() you have to increment it.
<?php
$array = array(1 => "a", 6 => "b", 19 => "c");
$rollback=0;
$max = sizeof($array);
for ($i=0; $i<$max; $i++) {
$key = key($array);
$value = $array[$key];
print "$key => $value\n";
next($array);
if ($value == "b" && $rollback == 0) {
prev($array);
$rollback = 1;
$max++;
}
}
This question already has answers here:
How to count the consecutive duplicate values in an array?
(6 answers)
Closed last month.
I'm stuck and am wondering if someone could point me in the right direction.
I have an array containing numbers, eg:
$start = array(0,0,0,45,45,0,3,0,0,1,1,1,1);
And would like that array to convert to this array:
$result = array( array('id'=>0, 'aantal'=>3,
array('id'=>45,'aantal'=>2),
array('id'=>0, 'aantal'=>1),
array('id'=>3,'aantal'=>1),
array('id'=>0, 'aantal'=>1),
array('id'=>1,'aantal'=>4)
)
I tried traversing the $start array, but I got stuck onlooking up the n-1 in $start without having the key.
Does anyone have any advice on how I can do this?
This would be the typical approach for run length encoding an array of items:
$array = array(0,0,0,45,45,0,3,0,0,1,1,1,1);
$last = null;
$current = null;
$result = array();
foreach ($array as $item) {
if ($item == $last) {
// increase frequency by 1
++$current['aantal'];
} else {
// the first iteration will not have a buffer yet
if ($current) {
$result[] = $current;
}
// create buffer array item, set frequency to 1
$current = array('id' => $item, 'aantal' => 1);
$last = $item;
}
}
// last pass
if ($current) {
$result[] = $current;
}
Given associated array of messages (each 2nd level array is a result of different sql query):
$tmp = array(
'type1'=>array ('key'=>'value'),
'type2'=>array(1=>1,2=>2,3=>3,4=>'men',5=>'pro'),
'type3'=>array(1=>1,2=>2,3=>3,'test'=>'website','creation'=>'www.prost.pro',8,9,10=>array('3dlevel','subarray'),11,12,13,14,15,16,18,18,19,20),
'type4'=>array(1,2,3)
);
I need to display only 8 of them.
And they have to represent all and every types (1st level values) of messages.
So I need to call:
$total_quantity_limit = 8;
var_dump(array_assoc_truncate($tmp, $total_quantity_limit));
And to get something like:
array(
'type1'=>array('key'=>'value'),
'type2'=>array(1=>1,2=>2,3=>3),
'type3'=>array(1=>1,2=>2),
'type4'=>array(1,2)
);
what have to be inside array_assoc_truncate()?
From the example output I see, looks like you want something like:
<?php
$tmp = array(
'type1'=>array('key'=>'value'),
'type2'=>array(1=>1,2=>2,3=>3,4=>'men',5=>'pro'),
'type3'=>array(1=>1,2=>2,3=>3,'test'=>'website','creation'=>'www.prost.pro',8,9,10,11,12,13,14,15,16,18,18,19,20),
'type4'=>array(1,2,3)
);
function array_assoc_truncate($array, $limit){
$depth = 0;
$count = 0;
$out = array();
//outter loop
while($count < $limit){
//boolean, was a key found
$found = false;
//loop through each top level key
foreach($array as $k=>$v){
//if the value is an array
if(is_array($v)){
//get the keys
$keys = array_keys($v);
//if a keys exists at this depth
if(isset($keys[$depth])){
//get the key
$key = $keys[$depth];
//if $out doesn't have the top level key yet, add it
if(!isset($out[$k])){
$out[$k]=array();
}
//set the value under $key in $out
$out[$k][$key]=$v[$key];
//increment our count
$count++;
//a key was found at this depth
$found=true;
}
}
//if we hit our limit, break
if($count >= $limit){
break;
}
}
//if no key was found at this depth, there is no more depth to search
if(!$found){
break;
}
//go down one more level
$depth++;
}
//return the output array
return $out;
}
echo '<hr><h1>Before:</h1>';
var_dump($tmp);
echo '<hr><h1>After:</h1>';
adump(array_assoc_truncate($tmp, 8));
http://codepad.viper-7.com/a8cF5J
However, as hinted at above, if this is from the result of a query, you could/should probably restructure your query to give you better results.