Shifting the items in an array by a specified value - php

How would you go about circularly rotating the items in an array up or down by a specified value. For example
$value = 1; // circularly rotate by 1
$array = array(1,2,3,4,5);
// Should return
array(2,3,4,5,1);
The entire array is circularly rotated anti-clockwise by 1. 1 went to the end and 2 became the leading number in the array. I cannot find a reliable way to do this.

Use a for-loop with a specification on how many items you want moved, and array_shift() to shift the array. Then add the first element to a shifted array (which essentially moves the first item to the last element)
$shift = 2; // How many times you want to move it
$output = array(1, 2, 3, 4, 5);
for ($i = 0; $i < $shift; $i++) {
array_push($output , array_shift($output));
}
print_r($output); // 3, 4, 5, 1, 2
Live demo
References
http://php.net/manual/en/function.array-shift.php
http://php.net/manual/en/function.array-push.php

You can combine the array_push function that adds a value to the end of an array and the array_shift function that removes and returns the first element of an array.
<?php
$value = 1; // circularly rotate by 1
$array = array(1,2,3,4,5);
while ($value) {
array_push($array, array_shift($array));
$value--;
}
print_r($array);
?>

Related

randomly subtract desired percentage from a array using php

I have a array which contains 40 elements like this
$i = array('1','2','3','4','5','6','7','8','9','10','11',
'12','13','14','15','16','17','18','19','20','21','22','23','24',
'25','26','27','28','29','30','31','32','33','34','35',
'36','37','38','39','40');
now i have 4 variations like 5%, 10%, 15%, 20%
The requirement is i need to take array values randomly after subtracting the desired variation.
Lets say i used 5% variation so i need to separate 5% from the array ie 2 elements i need to remove randomly from the array and keep the rest 38 element in a new array randomly.so both the resulted array as well subtracted elements need to be in two different array.
I need a function with two parameter ie one is variation and another is required array ie resultant array or subtracted elements array.
This same sequence follows to all other variations.
Although I am not sure what you mean by rest 38 element in a new array randomly e.g. does this mean the new array is also shuffled? This is what I came up with.
<?php
function splitArray($variation, $array) {
$count = count($array); // Count the elements in the given array
$removeNumber = floor($count*($variation/100)); // Calculate the number of elements to remove
// Create an array holding the index numbers for splicing, these numbers are random.
for($i=0; $i<$removeNumber; $i++) {
$removeArray[] = rand(0,$count-1);
}
// Loop through the removeArray to retrieve the indexes to splice at.
for($i=0; $i<count($removeArray); $i++) {
$subArray[] = $array[$removeArray[$i]];
array_splice($array, $removeArray[$i], 1);
}
// return the newly spliced array and the spliced items array
return array($array, $subArray);
}
$oldArray = array('1','2','3','4','5','6','7','8','9','10','11',
'12','13','14','15','16','17','18','19','20','21','22','23','24',
'25','26','27','28','29','30','31','32','33','34','35',
'36','37','38','39','40');
$array = splitArray(5, $oldArray);
$subArray = $array[0];
$newArray = $array[1];
<?php
function subtractFromArray($array,$percentage){
//Randonmising the array
shuffle($array);
//percentage numeric equivalent wrt array aize
$substract_variation_count = floor(sizeof($array) * $percentage/100);
//New extracted array
$new_array = array_slice($array,$substract_variation_count);
//retuns the new array with 38 elements
return $new_array;
}
//array with all elements
$array = array('1','2','3','4','5','6','7','8','9','10','11',
'12','13','14','15','16','17','18','19','20','21','22','23','24',
'25','26','27','28','29','30','31','32','33','34','35',
'36','37','38','39','40');
//get new array
$new_array = subtractFromArray($array,5);
//print new array
print_r($new_array);
//Substracted array with 2 elements
$substrcated_array = array_diff($array,$new_array);
print_r($substrcated_array);
?>

What's the most efficient way to array_pop() the last n elements in an array?

What's an efficient way to pop the last n elements in an array?
Here's one:
$arr = range(1,10);
$n = 2;
$popped_array = array();
for ($i=0; $i < $n; $i++) {
$popped_array[] = array_pop($arr);
}
print_r($popped_array); // returns array(10,9);
Is there a more efficient way?
Use array_splice():
If you're trying to remove the last n elements, use the following function:
function array_pop_n(array $arr, $n) {
return array_splice($arr, 0, -$n);
}
Demo
If you want to retrieve only the last n elements, then you can use the following function:
function array_pop_n(array $arr, $n) {
array_splice($arr,0,-$n);
return $arr;
}
Demo
It's important to note, looking at the other answers, that array_slice will leave the original array alone, so it will still contain the elements at the end, and array_splice will mutate the original array, removing the elements at the beginning (though in the example given, the function creates a copy, so the original array still would contain all elements). If you want something that literally mimics array_pop (and you don't require the order to be reversed, as it is in your OP), then do the following.
$arr = range(1, 10);
$n = 2;
$popped_array = array_slice($arr, -$n);
$arr = array_slice($arr, 0, -$n);
print_r($popped_array); // returns array(9,10);
print_r($arr); // returns array(1,2,3,4,5,6,7,8);
If you require $popped_array to be reversed, array_reverse it, or just pop it like your original example, it's efficient enough as is and much more direct.
Why not use array_slice. You can give a start and a length, so if you do 2 from the end you will get the last two items in the array:
$arr = range(1,10);
$n = 2;
$start = count($arr) - $n;
print_r(array_slice($arr, $start, $n));
Thanks for the array_slice comments. I don't know why that didn't immediately come to mind.
It looks (to me) like the easiest way is:
$arr = range(1,10);
$n = 2;
$popped_array = array_slice($arr,-$n);
print_r($popped_array); // returns array(10,9);

create another multi dimensional array from an array

Suppose i have an array
$x= ('A'=>31, 'B'=>12, 'C'=>13, 'D'=>25, 'E'=>18, 'F'=>10);
I need to generate an array somewhat like this
$newx = (0 => array('A'=>31 , 'B' =>1) , 1 => array('B'=>11 , 'C' =>13 , 'D'=>8) , 2 =>array('D'=>17 , 'E'=>15) , 3=>array('E'=>3,'F'=>10);
Now in this case each value of $newx has to be = 32 and this is how it will work $x[A] = 31 , $x[B] = 12 so first of all we have to make the sum quantity to be 32 keeping the index same for the new array i.e
array(0=>array('A'=>31,'B'=>1) , 1=>array('B'=>11) )
the process should continue for each value of $x.
while I'm pretty sure this is a homework assignment and well, you really should provide code of your own, at least try to, I found the thing amusing so I went ahead and gave it a try. I guess I'll be downvoted for his and I probably do deserve it, but here goes anyway.
What you need to do is:
loop through your array,
determine the elements that give you 32 and then store that result in the final array.
subtract the value of the last element from your result from the corresponding element of your working array
shrink your array next by deleting the first elements until the very first element of the array you're still working with equals the last element your last result returned.
if your last result < 32, quit.
With this in mind, please try to find a solution yourself first and don't just copy-paste the code? :)
<?php
$x = array('A'=>31, 'B'=>12, 'C'=>13, 'D'=>25, 'E'=>18, 'F'=>10);
$result = array();
function calc($toWalk){
// walk through the array until we have gathered enough for 32, return result as an array
$result = array();
foreach($toWalk as $key => $value){
$count = array_sum($result);
if($count >= 32){
// if we have more than 32, subtract the overage from the last array element
$last = array_pop(array_keys($result));
$result[$last] -= ($count - 32);
return $result;
}
$result[$key] = $value;
}
return $result;
}
// logic match first element
$last = 'A';
// loop for as long as we have an array
while(count($x) > 0){
/*
we make sure that the first element matches the last element of the previously found array
so that if the last one went from A -> C we start at C and not at B
*/
$keys = array_keys($x);
if($last == $keys[0]){
// get the sub-array
$partial = calc($x);
// determine the last key used, it's our new starting point
$last = array_pop(array_keys($partial));
$result[] = $partial;
//subtract last (partial) value used from corresponding key in working array
$x[$last] -= $partial[$last];
if(array_sum($partial) < 32) break;
}
/*
reduce the array in size by 1, dropping the first element
should our resulting first element not match the previously returned
$last element then the logic will jump to this place again and
just cut off another element
*/
$x = array_slice($x , 1 );
}
print_r($result);

Filter array to retain rows with smallest element count and unique first and last elements

I want to remove rows from my array so that my result is an array that contains rows with unique first and last elements. If two (or more) rows have the same first and last value, I want to preserve the row with the lowest element count.
Say I have the following array:
$var = [
[1, 2, 3],
[1, 3],
[1, 2, 4, 3],
[1, 3, 4]
];
What I want is to remove all arrays from $var that have the first and last element the same as another array from $var but have more elements.
Because the first three rows all start with 1 and end with 3, only the second row containing [1, 3] should be kept.
The fourth row ([1, 3, 4]) uniquely starts with 1 and ends with 4, so it should also be kept.
The output should be:
[
[1, 3],
[1, 3, 4]
]
I am looking for the most efficient way of doing this, both in terms of memory and time. $var may have up to 100 arrays, and each individual array may have up to 10 elements in it. I thought of using some kind of comparison between all two elements (for(i=0;...) for(j=i+1;...) complexCompareFunction();), but I believe this isn't very efficient.
use current and end
$all = array();
foreach ($var as $idx=>$arr):
$first = current($arr);
$last = end($arr);
$size = count($arr);
$key = $first.'.'.$last;
if (isset($all[$key])):
if ($size > $all[$key]):
unset($var[$idx]);
else:
$all[$key] = $size;
endif;
else:
$all[$key] = $size;
endif;
endforeach;
ops ... you can iterate (again) at the end to ensure the already reduced sized array can be further removed
In general, yes, you are too worried about efficiency (as you wondered in another comment). Though PHP is not the most blisteringly-fast language, I would suggest building the most straightforward solution, and only worry about optimizing it or streamlining it if there is a noticeable issue with the end result.
Here is what I would do, off the top of my head. It is based off of ajreal's answer but hopefully will be easier to follow, and catch some edge cases which that answer missed:
// Assume $var is the array specified in your question
function removeRedundantRoutes( $var ){
// This line sorts $var by the length of each route
usort( $var, function( $x, $y ){ return count( $x ) - count( $y ); } );
// Create an empty array to store the result in
$results = array();
// Check each member of $var
foreach( $var as $route ){
$first = $route[0];
$last = $route[ count( $route ) - 1 ];
if( !array_key_exists( "$first-$last", $results ) ){
// If we have not seen a route with this pair of endpoints already,
// it must be the shortest such route, so place it in the results array
$results[ "$first-$last" ] = $route;
}
}
// Strictly speaking this call to array_values is unnecessary, but
// it would eliminate the unusual indexes from the result array
return array_values( $results );
}
Here is how I would group by a temporary key (formed by creating a delimited string from the first and last value in a given row) and conditionally push qualifying data into a result array. When the loop finishes, extract the second column from the result array to produce an indexed array containing only the smallest of qualifying rows. No pre-sorting required.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$cache = [count($row), $row];
array_splice($row, 1, -1);
$key = implode('-', $row);
if (!isset($result[$key]) || $cache[0] < $result[$key][0]) {
$result[$key] = $cache;
}
}
var_export(array_column($result, 1));
Alternative Code: (Demo)
$result = [];
foreach ($array as $row) {
$count = count($row);
$key = $row[0] . '-' . $row[array_key_last($row)]; // or array_pop($row)
if (!isset($result[$key]) || $count < $result[$key][0]) {
$result[$key] = [$count, $row];
}
}
var_export(array_column($result, 1));
Output:
array (
0 =>
array (
0 => 1,
1 => 3,
),
1 =>
array (
0 => 1,
1 => 3,
2 => 4,
),
)

Efficient algorithm for detecting matches

I'm looking for an efficient algorithm for detecting equal values in an array of integers N size. It must return the indices of the matches.
Alas, I can't think of anything more clever then brute force with two loops.
Any help will be appreciated.
Thanks!
You could intersect the array. This finds all the values of array2 that are in array1
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array);
Would return
Array
(
[a] => green
)
It returns an array with all of the keys and values of the matches. Basically you can provide an infinite number of arguments to the array_insert_assoc:
array_intersect_assoc($base_array, $arr1, $arr2 ...);
It will search $base_array for the values that are in all the subsequent arrays. That means that the key and value will be taken from the $base_array
You could also compare the keys by using:
array_intersect_keys($base_array, $arr1, $arr2, $arr3);
These loops are O(N^2). Is N big? If so, can you sort the array O(NlogN), then scan it O(N)? ... or am I missing something?
You can use a set to hold the recent values. For example,
results = empty list
set = empty set
foreach key, val in array:
if val is not in set: add val to set
else: add key to results
return results
Each look up of set is O(1), so this algo will results in O(n) instead of O(n^2) if nested-loop is used.
In case you want to keep track of multi-occurence like this array 1, 2, 3, 3, 2, 1 you can use a hash table with key is the value and value (of the corresponding key in table) is the list of indices. The result for the given array will look lik {1:0, 5; 2: 1, 4; 3: 2, 3}.
results = empty hashtable
for each key, val in array:
if val is not in results:
results[val] = new list()
results[val].append(key)
return results
Perhaps this?
$arr = array_map('unserialize', array_unique(array_map('serialize', $arr)));
From the question: How to remove duplicated 2-dimension array in PHP?
if ($arr !== array_map('unserialize', array_unique(array_map('serialize', $arr))))
{
// found duplicates
}
You don't have to go through all the array again for each element. Only test an element with the subsequent element in the array:
$array = /* huge array */;
$size = count($array);
for($i = 0; $i < $size; $i++)
{
for($j = $i + 1; $j < $size; $j++) // only test with the elements after $i
{
if($array[$i] == $array[$j])
return true; // found a duplicate
}
return false; // found no duplicate
}
That's the most efficient way I can think of. Adapt it to your need as you will.
If one of your arrays is reasonably static (that is you are comparing to the same array several times ) you could invert it.
That is set up another array which is keyed by value and returns the index into the real array.
$invert = array();
foreach ($cmptoarray as $ix => $ival) {
$invert[$ival] = $ix;
}
Then you simply need an if ( isset($invert[$compfrmarray[$i]) ) .... to check the number.
Note: this is only worth doing if you compare against the same array several times!
Just use an associative array mapping a value to its index:
foreach($array1 as $index => $value) {
$aa[$value] = $index;
}
foreach($array2 as $index => $value) {
if(isset($aa[$value])) {
echo 'Duplicate: . Index 1: '.$aa[$value].' Index 2: '.$index.'.';
}
}

Categories