PHP Re-order a numerically indexed array - php

I need to reorder a numerically indexed array arbitrarily. Take the following array which represent pages in a document:
$array[0 => 0
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10 ];
And a second array that defines the new order for specific elements (whilst automatically re-ordering those it would be conflicting with):
$reordered[3 => 4,
7 => 9,
6 => 1 ];
I need to have a resulting array that looks like:
$array[0 => 0
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 1,
7 => 9,
8 => 7,
9 => 8,
10 => 10 ];
Note how where an element is being replaced, the others are re-indexed (whilst respecting the configuration of the $reordered array).
What is the most elegant way to achieve this?

The following code does the job:
asort($reordered);
foreach ($reordered as $position => $item) {
$sliced = array_splice($array, array_search($item, $array), 1);
array_splice($array, $position, 0, $sliced);
}
print_r($array);
Outputs:
Array
(
[0] => 0
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 1
[7] => 9
[8] => 7
[9] => 8
[10] => 10
)
Or you can use reorder function from Nspl. It has the following signature:
reorder(array $list, $from, $to)
The solution will be:
use function nspl\a\reorder;
asort($reordered);
foreach ($reordered as $position => $item) {
$array = reorder($array, array_search($item, $array), $position);
}

try this : best way . I hope i solved your problem. use array_search($2, $array); gives 2 .to find and index to replace the value as given below.
<?php
$array=array(0=>0,1=>1,2=>2,3=>3,4=>4,5=>5,6=>6,7=>7,8=>8,9=>9);
$r=array(4=>2,5=>4,3=>6);
print_r($array);
echo "<br>";
$count=count($array);
foreach($r as $key =>$val){
$a=$array[$key];
$keyu = array_search($val, $array);
$array[$keyu]=$a;
$array[$key]=$val;
}
print_r($array);
?>
result:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 )
Array ( [0] => 0 [1] => 1 [2] => 5 [3] => 6 [4] => 2 [5] => 4 [6] => 3 [7] => 7 [8] => 8 [9] => 9 )

Its working Try this :
$old = range(100,110);
$order = array(3=>4,6=>1,7=>9);
function match($old,$order){
foreach ($old as $k => $v){
if(array_key_exists($k, $order)){
$new[] = $order[$k];
}else{
$new[] = $v;
}
}
return $new;
}
echo "<pre>";
print_r($old);
print_r($order);
print_r(match($old,$order));
OUTPUT :
Array
(
[0] => 100
[1] => 101
[2] => 102
[3] => 103
[4] => 104
[5] => 105
[6] => 106
[7] => 107
[8] => 108
[9] => 109
[10] => 110
)
Array
(
[3] => 4
[6] => 1
[7] => 9
)
Array
(
[0] => 100
[1] => 101
[2] => 102
[3] => 4
[4] => 104
[5] => 105
[6] => 1
[7] => 9
[8] => 108
[9] => 109
[10] => 110
)

Related

convert the array to the correct form

After converting the file
$rows = array_map('trim', file($lines1));
foreach ($rows as $key => $value) {
$params = array_map('trim', explode(';', $value));
}
an array of the following type is obtained, which is contained in a variable:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => i need this [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 )
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => i need this [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 )
etc...
how can you combine these arrays and put the values with the key = 3 into separate variables.
View:
Array ( [0] => i need this [1] => i need this and etc...
$range = range(1, 100);
$result = array_combine($range, $range);
check array_combine

php array remove n amount of items based on value

I have this array, there are 4 values = 3, how can i delete just two of them?
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 3
[7] => 2
[8] => 2
[9] => 2
[10] => 1
[11] => 2
[12] => 3
[13] => 2
)
I already tried unset(). is there any way to achieve this ?
so the array looks like this
Array (
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)
Just make two calls to unset, using array_search:
$array = array(1,2,3,3,3,4,5,6);
print_r($array);
unset($array[array_search(3, $array)]);
unset($array[array_search(3, $array)]);
print_r($array);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 3
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
Array
(
[0] => 1
[1] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
But this assumes that you are OK with the behavior of array_search, which would return the first index which matches the value 3. If you had some other order of removal in mind, then state it, and the code might have to be changed.
You might use a loop and skip the first 2 times you encounter a 3. Then for the next matches, you could use unset. Then you might use array_values to reset the keys:
$items = [
1,2,1,2,3,3,3,2,2,2,1,2,3,2
];
$found = 0;
foreach ($items as $key => $item) {
if ($item === 3) {
if ($found < 2) {
$found++;
continue;
}
unset($items[$key]);
}
}
print_r(array_values($items));
Result
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
[5] => 3
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 2
[11] => 2
)
Php demo

php array merge recursive with same array values and create combinations

Hello i am new in php and I just stuck in brainstorming array, I have below php array
[0] => Array
(
[0] => Array
(
[1] => 5
[2] => 12
[11] => 15
)
[1] => Array
(
[1] => 5
[2] => 12
[11] => 16
)
[2] => Array
(
[1] => 4
[2] => 9
[11] => 15
)
[3] => Array
(
[1] => 3
[2] => 9
[11] => 15
)
[4] => Array
(
[1] => 3
[2] => 9
[11] => 16
)
[5] => Array
(
[1] => 3
[2] => 13
[11] => 15
)
[6] => Array
(
[1] => 3
[2] => 13
[11] => 16
)
[7] => Array
(
[1] => 3
[2] => 12
[11] => 15
)
[8] => Array
(
[1] => 3
[2] => 12
[11] => 16
)
[9] => Array
(
[1] => 4
[2] => 9
[11] => 16
)
[10] => Array
(
[1] => 4
[2] => 13
[11] => 15
)
[11] => Array
(
[1] => 4
[2] => 13
[11] => 16
)
[12] => Array
(
[1] => 4
[2] => 12
[11] => 15
)
[13] => Array
(
[1] => 4
[2] => 12
[11] => 16
)
[14] => Array
(
[1] => 5
[2] => 9
[11] => 15
)
[15] => Array
(
[1] => 5
[2] => 13
[11] => 16
)
)
And I want below out put
[0]=> Array
(
[5]=> Array
(
[12]=> Array
(
[0] => 15
[1] => 16
)
[9]=> Array
(
[0] => 15
)
[13]=> Array
(
[0] => 16
)
)
[4]=> Array
(
[9]=> Array
(
[0] => 15
[1] => 16
)
[13]=> Array
(
[0] => 16
)
[12]=> Array
(
[0] => 15
[1] => 16
)
)
[3]=> Array
(
[9]=> Array
(
[0] => 15
[1] => 16
)
[13]=> Array
(
[0] => 15
[1] => 16
)
[12]=> Array
(
[0] => 15
[1] => 16
)
)
)
Sorry out put not much clear but I want check each array first common value e.g 5,4, and 3 then based on these value traverse though each value and create tree array
As Example simple i want each arrays first element and then recursively array check values and create new
Example have just 3 level but i want recursive n-level and , help would be Appreciate.
Not sure if I understand correctly as the samples are not explained clearly but I somehow was able to print the desired output. Explanations given thru comments
// I just simplified your given array
$input[0] = array(
[1 => 5, 2 => 12, 11 => 15],
[1 => 5, 2 => 12, 11 => 16],
[1 => 4, 2 => 9, 11 => 15],
[1 => 3, 2 => 9, 11 => 15],
[1 => 3, 2 => 9, 11 => 16],
[1 => 3, 2 => 13, 11 => 15],
[1 => 3, 2 => 13, 11 => 16],
[1 => 3, 2 => 12, 11 => 15],
[1 => 3, 2 => 12, 11 => 16],
[1 => 4, 2 => 9, 11 => 16],
[1 => 4, 2 => 13, 11 => 15],
[1 => 4, 2 => 13, 11 => 16],
[1 => 4, 2 => 12, 11 => 15],
[1 => 4, 2 => 12, 11 => 16],
[1 => 5, 2 => 9, 11 => 15],
[1 => 5, 2 => 13, 11 => 16]
);
// Declare new output array
$output[0] = [];
// Loop thru each array inside $input[0] array
foreach($input[0] as $key => $value) {
// Set each array value to variables
$one = $value[1];
$two = $value[2];
$eleven = $value[11];
// Check if $one key already exist, create if not
if (!isset($output[0][$one])) {
$output[0][$one] = [];
}
// Check if $two key already exist, create if not
if (!isset($output[$one][$two])) {
$output[0][$one][$two] = [];
}
// push each $eleven
$output[0][$one][$two][] = $eleven;
};
// Prints output array
echo '<pre>';
print_r($output);
To accommodate situations where you have multiple first-level elements, use a loop. Use another loop to iterate each entry. You can even use array destructuring inside the nested foreach() to declare variables.
Code: (Demo)
$result = [];
foreach ($input as $key => $array) {
foreach ($array as [1 => $one, 2 => $two, 11 => $eleven]) {
$result[$key][$one][$two][] = $eleven;
}
}
var_export($result);
There is no need to declare/assign parent keys to append data into them. There are deliberately no conditions in my snippet.

How to combine two array and chunk it if the key is same

I'am bulding an app with laravel, and I have a problem, I have two array in php :
Array1
(
[0] => 15
[1] => 15
[2] => 16
[3] => 16
[4] => 17
[5] => 17
[6] => 17
[7] => 17
)
Array2
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 0
[5] => 1
[6] => 2
[7] => 3
)
in this case i can not to use array_chunk because the value of array2 is dinamic and key of array1 must not be same if i combine it, so, how i can combine it to be like this :
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
Simple foreach loop:
$arr1 = [15,15,16,16,17,17,17,17];
$arr2 = [0,1,1,2,0,1,2,3];
$result = [];
foreach($arr1 as $k => $v){
$result[$v][] = $arr2[$k];
}
print_r($result);
The output:
Array
(
[15] => Array
(
[0] => 0
[1] => 1
)
[16] => Array
(
[0] => 1
[1] => 2
)
[17] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
)
$array1 =
[
0 => 15,
1 => 15,
2 => 16,
3 => 16,
4 => 17,
5 => 17,
6 => 17,
7 => 17,
];
$array2 =
[
0 => 0,
1 => 1,
2 => 1,
3 => 2,
4 => 0,
5 => 1,
6 => 2,
7 => 3,
];
$arr = array_unique($array1);
print_r($arr);
$newarray = [];
foreach($arr as $ar){
foreach($array1 as $key => $value){
if($ar == $value){
$newarray[$value][] =$array2[$key];
}
}
}
print_r($newarray);

Set array keys to value of another arrays values - PHP

I have the following:
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
I want to use the value of each of those as the key in:
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
I've tried this:
foreach ($iOrder as $i)
{
$pOrder[$i] = $pOrder[$p];
$p++;
}
I get this:
( [12] => 2 [10] => 6 [5] => 5 [4] => 7 )
Any thoughts?
Do you mean
$result = array_combine($keys, $values);
?
array_combine()
Frist array, $arr1,
( [0] => 3 [1] => 2 [2] => 12 [3] => 6 [4] => 8 [5] => 7 [6] => 9 [7] => 10 [8] => 5 [9] => 4 )
Second array, $arr2,
( [0] => 7 [1] => 2 [2] => 10 [3] => 3 [4] => 5 [5] => 6 [6] => 11 [7] => 9 [8] => 4 [9] => 8 )
If I understood correct, you want to use the values of $arr1 as keys in $arr2.
$values = array_values($arr1);
=> This gives you the values of $arr1.
you can use, array_combine($keys, $values);
so the resulting array would be,
$result_arr = array_combine(array_values($arr1), array_values($arr2));
However, it may not work as expected if the no of items in two arrays are different.

Categories