Reorder elements in array using php - php

I have an array of elements like this:
$data = array(1, 2, 3, 4);
How can I reorder for example, starting from second element to get 2, 3, 4, 1; or starting from third element to get 3, 4, 1, 2?

One solution is to use array_slice function to separate the two portions and combine them with array_merge:
$data = [1, 2, 3, 4];
$pos = 2;
$ordered = array_merge(
array_slice($data, $pos),
array_slice($data, 0, $pos)
);
// [3, 4, 1, 2]

You can use array_splice
$data = array(1,2,3,4);
$out = array_splice($data, 1, 3);
array_splice($data, 0, 0, $out);
print_r($data);

You could just array_shift() and then push the shifted value back on to the end of the Array:
$data = array(1,2,3,4);
$data[] = array_shift($data);

Related

How to get max element at each index while comparing two arrays?

I have two indexed arrays of identical length:
$first_array = [1,3,4,5,6];
$second_array = [5,2,1,7,9];
I need to generate a new array that consists of the higher value between the two elements at each index.
The output should be:
$output_array[5, 3, 4, 7, 9];
Super easy one-liner:
Pass both arrays to array_map(), as it synchronously loops through both sets of data, call max() on the two elements.
Code: (Demo)
$first_array = [1, 3, 4, 5, 6];
$second_array = [5, 2, 1, 7, 9];
var_export(array_map('max', $first_array, $second_array));
Output:
array (
0 => 5,
1 => 3,
2 => 4,
3 => 7,
4 => 9,
)
Try this way. demo
<?php
$first_array = array(1,3,4,5,6);
$second_array = array(5,2,1,7,9);
$return = array();
foreach($first_array as $key => $value){
if($first_array[$key] > $second_array[$key]){
$return[] = $first_array[$key];
}else{
$return[] = $second_array[$key];
}
}
print_r($return);

Add value from one element to other in php

This is probably a duplicate, but I couldn't find the answer I needed, maybe my wording is wrong.
Anyway, I have a two-dimensional array with hundreds of values, what I need is to insert a value from second element to the first element.
Example;
I have four elements in an array:
[0] = 1, [1] = 9, [2] = 9, [3] =5
I need to put these into a single element, so that it would turn
into this: [0] = 1995.
I have a feeling there might be something I could do with foreach, if so, maybe someone can explain to me, in detail, how that would exactly work?
OR
Maybe there's a function I'm not aware of.
You can use the following, using implode:
$arr = [1, 9, 9, 5];
$val = implode($arr);
unset($arr);
$arr[0] = $val;
demo: https://ideone.com/VTb7vO
To use the solution using implode on the whole multidimensional array, you can use the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
foreach ($arr as $key => $value) {
$val = implode($value);
$arr[$key] = $val;
}
demo: https://ideone.com/X6vueO
another, much shorter solution could be the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
$arr = array_map('implode', $arr);
demo: https://ideone.com/0ju8he
To concatenate each inner array of numbers you can use implode on each of it.
$newArray = array_map('implode', $array);
If it is executed on the array [[1, 2, 3], [1, 3], [1, 1, 1]] it will create the array ['123', '13', '111']. demo
$arr = [2,3,4];
$var = implode($arr, '');
var_dump( $var );
Two dimensional:
$cars = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
foreach ($cars as $val) {
$var1 = implode($val, '');
var_dump( $var1 );
}
$var= "";
foreach($arr as $element) {
$var .= $element;
}

Rewriting an Array keys after sorting it

$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
I have sorted the $arrs to change it to $arr3, $arr2, $arr1, My problem is that it kept its Array Key as it is, I want to rewrite these keys by its new order, so instead of
[2]$arr3 [1]$arr2 [0]$arr1
it becomes
[0]$arr3 [1]$arr2 [2]$arr1
I thought about explode()ing then implode()ing the array again But it didn't work because it is a MDArray like the following $arrs = implode(explode($arrs)); after arsort().
What is the best and shortest way to re[write][make] the array keys?
Just simply use array_values;
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
$arrs = array_values($arrs);
This will reset the keys based on the order.
You only need rsort if you don't need to keep key
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
rsort($arrs);
print_r($arrs);
DEMO
I think you need to use rsort() instead of arsort() because, the latter will preserve the indices when sorting, while the first will reorder the values regardless of the keys.

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

Limit multidimensional array to X arrays inside

I have a multidimentional array like this:
$my_array = array(
array($user_id, $id, $type, $log, $url, $timestamp),
array($user_id, $id, $type, $log, $url, $timestamp),
array($user_id, $id, $type, $log, $url, $timestamp),
// and so on...
);
$my_array has a random amount of arrays inside. I want to limit the amount of arrays to 5 inside $my_array. How can I achieve this? So, if there are 5 arrays inside, and one more is added, then remove the last array...
Just use a function to add arrays to it, then do your checks. E.g
function addToArray($arr, $newValue, $limit=5) {
if (count($arr) >= $limit) {
array_shift($arr);
}
$arr[] = $newValue;
return $arr;
}
The function array_shift removes the first element and returns the shortened array.
If it is possible for things to be added to the array outside of this function, you can generalize:
function addToArray($arr, $newValue, $limit=5) {
while (count($arr) >= $limit) {
array_shift($arr);
}
$arr[] = $newValue;
return $arr;
}
EDIT: Added improvements suggested by Jonathan Kuhn.
Add the new value and then call array_slice.
If you want to add to the end of the array:
//array with more than 5
$array = range(1, 8); // [1, 2, 3, 4, 5, 6, 7, 8]
//add the element
$array[] = 9;
//get the last 5
$array = array_slice($array, -5);
//output: [5, 6, 7, 8, 9]
http://codepad.viper-7.com/DL91ZK
If you want to add to the beginning of the array
//array with more than 5
$array = range(1, 8); // [1, 2, 3, 4, 5, 6, 7, 8]
//add to the array
array_unshift($array, 0);
//get the first 5
$array = array_slice($array, 0, 5);
//output: [0, 1, 2, 3, 4]
http://codepad.viper-7.com/RGfohJ

Categories