how can i compare an array values with subarray values - php

I try to get the differences between two arrays
I have one big array with subarray and a small simple array.
I want to get the difference from the big array.
I use this to get the differences between 2 arrays, but working with subarray is someting else.
$array3 = array_diff($bigArray, $smallArray);
$smallArray = Array
(
[0] => 2 (how i compare this values)
[1] => 3 (how i compare this values)
)
$bigArray = Array
(
[0] => Array
(
[g_id] => 2 (with this value)
[g_nume] => Arad I Betel
)
[1] => Array
(
[g_id] => 3 (with this value)
[g_nume] => Arad IV Agape
)
[2] => Array
(
[g_id] => 4 (with this value)
[g_nume] => Frumuseni
)
[3] => Array
(
[g_id] => 7 (with this value)
[g_nume] => Cuvin
)
)
And the result to be like this:
Array
(
[0] => Array
(
[g_id] => 4 (with this value)
[g_nume] => Frumuseni
)
[1] => Array
(
[g_id] => 7 (with this value)
[g_nume] => Cuvin
)
)

<?php
$disard_ids = [
'2', '3'
];
$items =
[
[
'id'=>'1'
],
[
'id'=>'2'
],
[
'id'=>'3'
],
[
'id'=>'4'
]
];
foreach ($items as $item) {
if(array_search($item['id'], $discard_ids) === false) {
$result[] = $item;
}
}
var_export($result);
Output:
array (
0 =>
array (
'id' => '1',
),
1 =>
array (
'id' => '4',
),
)

Related

how to Grouping array in php

I would like to know how to combine and overwrite the already existed array value in php.
my current array look like:
Array
(
[1149] => 3
[4108] => 5
)
As shown above values, i need expected result in php as below :
Array
(
[0] => Array
(
[offer_id] => 1149
[quantity] => 3
)
[1] => Array
(
[offer_id] => 4108
[quantity] => 5
)
)
How about:
$result = [];
foreach (
[
1149 => 3,
4108 => 3,
] as $key => $value
) {
$result[] = [
'offer_id' => $key,
'quantity' => $value,
];
}
print_r($result);

Merge all subarrays on second level of a multidimensional array [duplicate]

This question already has answers here:
How to array_merge_recursive() an array?
(2 answers)
Closed 5 months ago.
I need to find a way to combine elements by key in a new array, such that I end up with just one key containing all values for that key (found elsewhere in the parent array). This will make more sense when you see the example below:
Array
(
[0] => Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
)
[1] => Array
(
[AAA] => Array
(
[0] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
)
The original array is quite large with more keys and values. But everything is in the same structure depicted above.
I'd like to read that in, and return
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[4] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
What's the least painful and most efficient way of accomplishing this?
Using array_merge_recursive() and array unpacking to pass the separate parts into it...
print_r(array_merge_recursive(...$array));
gives...
Array
(
[AAA] => Array
(
[0] => F104_f58f85f9dbbe1ce083ca09a6f9ace679
[1] => F104_480e9f231b2b5a138f8aa5570ffa8634
[2] => F104_d5fde0ad2499052e0eae6dec451f9385
[3] => F104_480e9f231b2b5a138f8aa5570ffa8634
)
[BBB] => Array
(
[0] => F44_e2e052e7b78abbae02ffcf7413302a0c
[1] => F44_4d4da736509babebba7433b203b16753
)
[ZZZ] => Array
(
[0] => F20_5e742bc21f41a14243c3f72b9fa4815e
)
)
You could use array_map() to traverse the parent array. And use another foreach loop to combine the values. Please note the $output array is passed by reference.
$inputArray = [
0 => [
'AAA' => [
0 => 'F104_f58f85f9dbbe1ce083ca09a6f9ace679',
1 => 'F104_480e9f231b2b5a138f8aa5570ffa8634',
2 => 'F104_d5fde0ad2499052e0eae6dec451f9385',
],
'BBB' => [
0 => 'F44_e2e052e7b78abbae02ffcf7413302a0c',
1 => 'F44_4d4da736509babebba7433b203b16753',
]
],
1 => [
'AAA' => [
0 => 'F104_480e9f231b2b5a138f8aa5570ffa8634'
],
'ZZZ' => [
0 => 'F20_5e742bc21f41a14243c3f72b9fa4815e'
]
]
];
$output = [];
array_map(function($values) use (&$output){
foreach ($values as $key => $value){
if(empty($output[$key])){
$output[$key] = $value;
}else{
$output[$key] = array_merge($output[$key], $value);
}
}
}, $inputArray);
print_r($output);
Here is a working example http://sandbox.onlinephpfunctions.com/code/97909ce93184fad6c33af8eb48d184ae86438774

Convert Multidimensional arrays as key and value in PHP

I have 2 Multidimensional arrays as follow:
Array1:
Array (
[0] => Array (
[0] => 2D Design
[1] => 3D Design & Modeling)
[1] => Array ( [0] => Android Developer
[1] => Artificial Intelligence
[2] => Web Developer)
)
Array2:
Array (
[0] => Array (
[0] => 5
[1] => 10)
[1] => Array ( [0] => 2
[1] => 4
[2] => 6)
)
I want to combine the above 2 arrays as key and value as below.
Array (
[0] => Array (
[2D Design] => 5
[3D Design & Modeling] => 10 )
[1] => Array (
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6 )
)
Please help me to do this. Answers will be appreciated.
use array_combine() function creates an array by using the elements from one "keys" array and one "values" array.
Note: Both arrays must have equal number of elements!
First parameter array taken as key of new array and second parameter taken as value new array .
$new_array=array();
for($i=0;$i<count($arr1);$i++)
{
$new_array[$i]=array_combine($arr1[$i],$arr2[$i]);
}
print_r($new_array);
Output :
Array
(
[0] => Array
(
[2D Design] => 5
[3D Design & Modeling] => 10
)
[1] => Array
(
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6
)
)
This will work,
$arr1 = array(
0 => array(
0 => "2D Design",
1 => "3D Design & Modeling"),
1 => array(0 => "Android Developer",
1 => "Artificial Intelligence",
2 => "Web Developer",
),
);
$arr2 = array(
0 => array(
0 => 5,
1 => 10,
),
1 => array(0 => 2,
1 => 4,
2 => 6,
),
);
$temp = [];
foreach ($arr1 as $k => &$v) {
foreach ($v as $k1 => &$v1) {
$temp[$k][$v1] = $arr2[$k][$k1];
}
}
print_r($temp);
I have fetched values of first array arr1 as key to temp variable and map it with values of arr2as value to temp array.
This code will work even if index i.e. 0,1,2,3 can be anything.
Here is working code.
Simply make mapped calls of array_combine(). So long as the same positioned rows have the same number of elements in them, everything will work perfectly.
Code: (Demo)
$keys =[
['2D Design', '3D Design & Modeling'],
['Android Developer', 'Artificial Intelligence', 'Web Developer']
];
$values = [
[5, 10],
[2, 4, 6]
];
var_export(
array_map('array_combine', $keys, $values)
);

PHP merge arrays by value and merge it's sub arrays

I thought I have some understanding of arrays. But it looks like I have no understanding. Or my head don't want to work. I have arrays:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
)
)
)
[3] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1437082860000,1
)
)
)
[4] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437081840000,9
)
)
)
And here is what I want to achieve:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
[1] => 1437082860000,1
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
[1] => 1437081840000,9
)
)
)
How could I remove duplicates and merge data?
The sample input doesn't offer any variation in the data contained in the deept subarrays, so I'll demonstrate the utility of my snippet by adding a little more complexity in the data.
Effectively, each time a new key is encountered, you declare a new row containing the key element and a values element with an empty array. To identify that key as "encountered", the new row is assigned to the result array using the key value as the first level key.
Now that the values subarray is guaranteed to be declared, an inner loop will comb through the deeper arrays and push one or more (all) deep values into the respective values subarray -- in a flat fashion.
Code: (Demo)
$array = [
['key' => 'Person 1', 'values' => [['1436821440000,12']]],
['key' => 'Person 2', 'values' => [['1437562620000,24']]],
['key' => 'Person 3', 'values' => [['1437080040000,10']]],
['key' => 'Person 1', 'values' => [['1437082860000,1'], ['1437082860000,2', '1437082860000,3']]],
['key' => 'Person 3', 'values' => [['1437081840000,9']]],
];
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['key']])) {
$result[$row['key']] = ['key' => $row['key'], 'values' => []];
}
foreach ($row['values'] as $values) {
array_push($result[$row['key']]['values'], ...$values);
}
}
var_export(
array_values($result)
);
Output:
array (
0 =>
array (
'key' => 'Person 1',
'values' =>
array (
0 => '1436821440000,12',
1 => '1437082860000,1',
2 => '1437082860000,2',
3 => '1437082860000,3',
),
),
1 =>
array (
'key' => 'Person 2',
'values' =>
array (
0 => '1437562620000,24',
),
),
2 =>
array (
'key' => 'Person 3',
'values' =>
array (
0 => '1437080040000,10',
1 => '1437081840000,9',
),
),
)
$result_arr = array();
foreach ($arr as $sub_arr)
{
$result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
}

Array merge on key of two associative arrays in php?

How can I merge these two array together?
Array
(
[0] => Array
(
[id] => 5
[cnt] => 14
)
[1] => Array
(
[id] => 8
[cnt] => 2
)
)
Array
(
[0] => Array
(
[id] => 8
[binding] => hardcover
)
[1] => Array
(
[id] => 5
[binding] => softcover
)
)
The expected result is:
Array
(
[0] => Array
(
[id] => 5
[binding] => softcover
[cnt] => 14
)
[1] => Array
(
[id] => 8
[binding] => hardcover
[cnt] => 2
)
)
The merge of these two array should happen on the [id] value and not on any sort of the array. How can I do this with php in a fast way?
$output = array();
$arrayAB = array_merge($arrayA, $arrayB);
foreach ( $arrayAB as $value ) {
$id = $value['id'];
if ( !isset($output[$id]) ) {
$output[$id] = array();
}
$output[$id] = array_merge($output[$id], $value);
}
var_dump($output);
Optionally if you want to reset output's keys, just do:
$output = array_values($output);
Merge the input arrays, then
Loop the rows and merge associative row data with the array union operator (+). The array union operator should only be used with associative, non-numeric keyed arrays.
The first time that a given id is encountered, there will be no "group" in the result array yet. To avoid a Warning generated by trying to merge row data with an undeclared variable, use the null coalescing operator (??) to fallback to an empty array.
The snippet below will be highly efficient because it makes no iterated function calls.
If you do not want the first level keys in the result array, then call array_values() to re-index the array.
Code: (Demo)
$a1 = [
['id' => 5, 'cnt' => 14],
['id' => 8, 'cnt' => 2],
];
$a2 = [
['id' => 8, 'binding' => 'hardcover'],
['id' => 5, 'binding' => 'softcover'],
];
$result = [];
foreach (array_merge($a1, $a2) as $row) {
$result[$row['id']] = ($result[$row['id']] ?? []) + $row;
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'id' => 5,
'cnt' => 14,
'binding' => 'softcover',
),
1 =>
array (
'id' => 8,
'cnt' => 2,
'binding' => 'hardcover',
),
)

Categories