php array merge recursive with same array values and create combinations - php

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.

Related

Trying To Get a Custom Array With Looping PHP

Good day, i am trying to generate a csv with array below
array[0] => array(
["a"] => 1,
["b"] => 1,
["c"] => 1, --> the value is only 1 and 2
["d"] => user2,
),
array[1] => array(
["a"] => 1,
["b"] => 1,
["c"] => 2,
["d"] => user3,
),
array[2] => array(
["a"] => 2,
["b"] => 2,
["c"] => 1,
["d"] => user4,
),
array[3] => array(
["a"] => 2,
["b"] => 2,
["c"] => 2,
["d"] => user5,
)
edit: i want to get the 2 array with same number on "a" and "b" value, but different in "c" value
i tried my code below
$results = array();
for($i = 1 ; $i <= 10 ; $i++){
$j = $i+1;
$response = array(
array(
$i, $i, 1, "user".$i
),
array(
$i, $i, 2, "user".$j
)
);
array_push($results, $response);
}
but the array result is not as i wanted like above result. is there any solutions?, sorry for the confusion of this question, i am trying my best to ask. thank you for the help!.
1.You need to use % (modules operator) in your code to set 1 or 2 at third index of each of your child-array.
2.Don't create too many array/variables in your code.Do direct assignments
$results = array();
$j = 1;
for($i=1; $i<=10; $i++){
$j++;
$results[] = array($i, $i, 1, "user".$j);
$j++;
$results[] = array($i, $i, 2, "user".$j);
}
print_r($results);
Output: https://3v4l.org/GE7MD
I think you will have to work with a for loop and a manually increments value for your userx value like this.
$results = [];
$user = 1;
for($i = 1 ; $i <= 10 ; $i++){
$x = 1;
$user++;
$results[] = [$i, $i, $x, "user$user"];
$x = 2;
$user++;
$results[] = [$i, $i, $x, "user$user"];
}
print_r($results);
RESULT:
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => user2
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => user3
)
[2] => Array
(
[0] => 2
[1] => 2
[2] => 1
[3] => user4
)
[3] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => user5
)
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
[3] => user6
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 2
[3] => user7
)
[6] => Array
(
[0] => 4
[1] => 4
[2] => 1
[3] => user8
)
[7] => Array
(
[0] => 4
[1] => 4
[2] => 2
[3] => user9
)
[8] => Array
(
[0] => 5
[1] => 5
[2] => 1
[3] => user10
)
[9] => Array
(
[0] => 5
[1] => 5
[2] => 2
[3] => user11
)
[10] => Array
(
[0] => 6
[1] => 6
[2] => 1
[3] => user12
)
[11] => Array
(
[0] => 6
[1] => 6
[2] => 2
[3] => user13
)
[12] => Array
(
[0] => 7
[1] => 7
[2] => 1
[3] => user14
)
[13] => Array
(
[0] => 7
[1] => 7
[2] => 2
[3] => user15
)
[14] => Array
(
[0] => 8
[1] => 8
[2] => 1
[3] => user16
)
[15] => Array
(
[0] => 8
[1] => 8
[2] => 2
[3] => user17
)
[16] => Array
(
[0] => 9
[1] => 9
[2] => 1
[3] => user18
)
[17] => Array
(
[0] => 9
[1] => 9
[2] => 2
[3] => user19
)
[18] => Array
(
[0] => 10
[1] => 10
[2] => 1
[3] => user20
)
[19] => Array
(
[0] => 10
[1] => 10
[2] => 2
[3] => user21
)
)
I removed the array_push() as it is quicker (no function call) to do a simple $arr[] = $something;

Creating multi-dimensional array from multiple distinct arrays

I suspect this question has been answered before but i have dug and dug this great forum for an answer in vain.....
I have 3 arrays that looks like this:
Array
(
[1] => 19
[2] => 2
[3] => 2018
)
Array
(
[1] => 19
[2] => 1
[3] => 2017
)
Array
(
[1] => 18
[2] => 2
[3] => 2016
)
I would like to convert this 3 arrays into a multidimensional array to look something like this:
$mynewArray = Array(
[0] =>array(
[1] => 19
[2] => 2
[3] => 2018
)
[1] =>array(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => array(
[1] => 18
[2] => 2
[3] => 2016
)
)
How do i achieve this in Php?
Demo Link.
You just need to add it in parent array as below,
$arr1 = [1 => 19, 2 => 2, 3 => 2018];
$arr2 = [1 => 19, 2 => 1, 3 => 2017];
$arr3 = [1 => 18, 2 => 2, 3 => 2016];
$mynewArray = [$arr1,$arr2,$arr3];
print_r($mynewArray);
Output
Array
(
[0] => Array
(
[1] => 19
[2] => 2
[3] => 2018
)
[1] => Array
(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => Array
(
[1] => 18
[2] => 2
[3] => 2016
)
)
Also you can append your child arrays to the parent by
$array1 = array("1"=>"1","2"=>"2","3"=>"3");
$array2 = array("1"=>"1","2"=>"2","3"=>"3");
$newarray = array($array1,$array2);

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);

PHP Compare two multidimensional arrays

I have two multidimensional arrays like this:
Guest allow array
Array
(
[0] => 5
[1] => 2
[2] => 3
)
and second one like this
Array
(
[0] => Array
(
[property_id] => 6
[guest_allow] => 2
)
[1] => Array
(
[property_id] => 9
[guest_allow] => 3
)
[2] => Array
(
[property_id] => 62
[guest_allow] => 2
)
[3] => Array
(
[property_id] => 72
[guest_allow] => 3
)
[4] => Array
(
[property_id] => 76
[guest_allow] => 4
)
[5] => Array
(
[property_id] => 80
[guest_allow] => 5
)
[6] => Array
(
[property_id] => 84
[guest_allow] => 3
)
)
So I have to match guest array all values are present in second array as well as I have to check guest values is less than to second array of guest_allow. If there is not match single value return empty array. If match value so return only match value. I want return array like this:
Array
(
[0] => Array
(
[property_id] => 6
[guest_allow] => 2
)
[1] => Array
(
[property_id] => 9
[guest_allow] => 3
)
[2] => Array
(
[property_id] => 62
[guest_allow] => 2
)
[3] => Array
(
[property_id] => 72
[guest_allow] => 3
)
[4] => Array
(
[property_id] => 84
[guest_allow] => 3
)
[5] => Array
(
[property_id] => 76
[guest_allow] => 4
)
)
Is it possible return this type array? Thanks.
Assuming that $guestArr is your guest array and $secondArr is your second array, the solution would be like this:
foreach($secondArr as $key => $arr){
if(!in_array($arr['guest_allow'], $guestArr)){
unset($secondArr[$key]);
}
}
// display $secondArr array
var_dump($secondArr);
Here's the live demo.
$first = [1, 2, 3];
$second = [
['property_id' => 6, 'guest_allow' => 2],
['property_id' => 66, 'guest_allow' => 3],
['property_id' => 76, 'guest_allow' => 4],
['property_id' => 86, 'guest_allow' => 2]
];
$result = array_filter($second, function($el) use ($first) {
return in_array($el['guest_allow'], $first);
});

PHP Re-order a numerically indexed array

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
)

Categories