how to Grouping array in php - 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);

Related

PHP Sort multi-dimension array by keys

I have a multi dimension array like below:
Array
(
[1200] => Array
(
[B] => Array
(
[4] => Array
(
[Name] => 'Joe']
)
)
[A] => Array
(
[3] => Array
(
[Name] => 'Paul']
)
)
)
[1100] => Array
(
[F] => Array
(
[2] => Array
(
[Name] => 'Sam']
)
)
[D] => Array
(
[1] => Array
(
[Name] => 'Jane']
)
)
)
What I wish to achieve is having the 4 digit number 1100 and 1200 in order ascending, then I need the letters (B A) and (F D) also in order, and then the single digit number under them in order ascending too. I believe I'm looking at a multi dimension array but any help would be appreciated.
The below function might be what you're looking for. It recursively orders arrays by their key.
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
Example usage
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
$data = [
1200 => [
'B' => [
4 => [
'Name' => 'Joe'
]
],
'A' => [
3 => [
'Name' => 'Paul'
]
]
],
1100 => [
'F' => [
2 => [
'Name' => 'Sam'
]
],
'D' => [
1 => [
'Name' => 'Jane'
]
]
]
];
ksort_r($data);
print_r($data);
The above will output...
Array
(
[1100] => Array
(
[D] => Array
(
[1] => Array
(
[Name] => Jane
)
)
[F] => Array
(
[2] => Array
(
[Name] => Sam
)
)
)
[1200] => Array
(
[A] => Array
(
[3] => Array
(
[Name] => Paul
)
)
[B] => Array
(
[4] => Array
(
[Name] => Joe
)
)
)
)

Shift array elements one level to the right (down one level) when using foreach

Sorry if there's an obvious answer, but I've been trying for hours to google the solution and tried various ways. Basically I'm trying to shift an array within a multidimensional array one level to the right. The output array is to be fed into a program which only accepts the desired format of multidimensional array.
We have ($result):
Array (
[0] => Array (
[id] => 1
[data] => AAA
)
[1] => Array (
[id] => 2
[data] => BBB
)
[2] => Array (
[id] => 3
[data] => CCC
)
)
We want to insert a new array [test] in between the multidimensional array. Desired result:
Array (
[0] => Array (
[test] => Array (
[id] => 1
[data] => AAA
)
)
[1] => Array (
[test] => Array (
[id] => 2
[data] => BBB
)
)
[2] => Array (
[test] => Array (
[id] => 3
[data] => CCC
)
)
)
What I've got so far:
foreach ($result as $key => $value) {
$newarray[] = array($key => $value)
};
return $newarray;
Unfortunately the above inserts an incremental index instead of [test] where we want it to.
Can anyone help?
You could use a loop and index in the same array using the current key. Then update the value with a new array where using test as the key and $v as the value.
$arrays = [
0 => [
"id" => 1,
"data" => "AAA"
],
1 => [
"id" => 2,
"data" => "BBB"
],
2 => [
"id" => 3,
"data" => "CCC"
],
];
foreach ($arrays as $k => $v) {
$arrays[$k] = ['test' => $v];
};
print_r($arrays);
Result
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
Demo
Try this:
foreach ($result as $key => $value) {
$newarray[$key] = ['test' => $value];
};
This would be a working and flexible approach:
<?php
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
array_walk($data, function(&$entry) {
$entry = [
'test' => $entry
];
});
print_r($data);
The output obviously is:
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
$newArray = array_map(function ($value) {
return ['test' => $value];
}, $data);
var_dump($newArray);
https://secure.php.net/manual/en/function.array-map.php

PHP Filter array of arrays by multiple keys and group the arrays with matching keys

I have an array of arrays, and I want to filter that array by multiple key values, and group the arrays with matching keys if there are any. Example array:
Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
[2] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[3] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
[4] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
and let's say I want the arrays with matching value and quantity to be grouped in a new array
The desired output would be something like this:
Array
(
[11] => Array
(
[0] => Array
(
[id] => 1
[value] => 11
[quantity] => 14
)
[1] => Array
(
[id] => 2
[value] => 11
[quantity] => 14
)
)
[22] => Array
(
[0] => Array
(
[id] => 3
[value] => 22
[quantity] => 14
)
[1] => Array
(
[id] => 4
[value] => 22
[quantity] => 14
)
)
[23] => Array
(
[0] => Array
(
[id] => 5
[value] => 23
[quantity] => 15
)
)
)
I'm clueless on how to achieve this.
A simple foreach loop over your array to ceate a new array will suffice for this purpose
$new_arr = [];
foreach ($inArray as $arr ) {
$new_arr[$arr['value']][] = $arr;
}
// unset the original array if you are finished with it
// in case it is large and you could make better use
// of the memory for something else
unset($inArray);
If you want to group by the values of multiple keys of the inner arrays, you can join those values together to form the key in your result array.
foreach ($array as $item) {
// combine value and quantity, for example
$key = $item['value'] . '|' . $item['quantity'];
$result[$key][] = $item;
}
just pass an array References and a sort key
function sortBy(&$arr,$by){
$result=array();
foreach($arr as $value){
if(isset($value[$by]))
$result[$value[$by]][]=$value;
}
return $result;
}
Examples
$sorted=sortBy($yourArray,'value'); //by value
$sorted=sortBy($yourArray,'id'); //by Idx
$sorted=sortBy($yourArray,'quantity'); //quantity
You want to group the array keys passing, if I understood correctly.
I usually use the laravel collection library, because it's provided out of the box.
ALthoug, here's my contribution.
Let's try:
function groupArray( $array, $key, $remove = null )
{
$result = array();
foreach (array_unique(array_column($array, $key)) as $value) {
$result[$value] = array_map(function ( $item ) use ( $remove ) {
unset($item[$remove]);
return $item;
}, array_filter($array, function ( $item ) use ( $value, $key ) {
return $item[$key] === $value;
}));
}
return $result;
}
The above function does the job, first we get all the selected key values using the array_column function. THen we do a foreach in the array to filter the array data using the provided key and finally we remove the selected key, if necessary (just because the selected key will be the grouped array keys).
Usage:
$sample = array(
[
'id' => 1,
'value' => 11,
'quantity' => 14
],
[
'id' => 2,
'value' => 11,
'quantity' => 14
],
[
'id' => 3,
'value' => 22,
'quantity' => 14
],
[
'id' => 4,
'value' => 22,
'quantity' => 14
],
[
'id' => 5,
'value' => 23,
'quantity' => 14
],
);
$a = groupArray($sample, 'value', 'value');
$b = groupArray($sample, 'value');
$c = groupArray($sample, 'quantity');

how to group a multidimensional array using a condition in php?

I have the following array
Array
(
[0] => Array
(
[shop] => 3
[price] => 332.00
)
[1] => Array
(
[shop] => 1
[price] => 3335.00
)
[2] => Array
(
[shop] => 3
[price] => 235.00
)
[3] => Array
(
[shop] => 1
[price] => 402.50
)
[4] => Array
(
[shop] => 3
[price] => 332.00
)
)
I need to group using shop and get get the minimum price of each shop in the array.
The expected result is as follows
Array
(
[0] => Array
(
[shop] => 3
[price] => 235.00
)
[1] => Array
(
[shop] => 1
[price] => 402.50
)
)
How will I do it?
You need to use additional variable
<?php
$arr = Array
(
0 => Array
(
'shop' => 3,
'price' => 332.00
),
1 => Array
(
'shop' => 3,
'price' => 232.00
),
2 => Array
(
'shop' => 1,
'price' => 232.00
),
3 => Array
(
'shop' => 3,
'price' => 432.00
),
4 => Array
(
'shop' => 1,
'price' => 132.00
),
);
$filtered = array();
foreach($arr as $prices){
if(FALSE === isset($filtered[$prices['shop']]) || $filtered[$prices['shop']]['price'] > $prices['price']){
$filtered[$prices['shop']] = $prices;
}
}
$filtered = array_values($filtered);
print_r($filtered);
This is very fast example how you can achieve this
It's rather simple.
Create a new array where you will host your stores as keys, and prices as values. What you want to do is to go through each element and first if the key does not exists in your new array, add it and its value. If however the key already exists, check if the current value is lower, and save it if true.
$grouped = [];
foreach ($arr as $k => $v) {
foreach ($k as $key => $value) {
if (isset($grouped[$key])) {
if ($value < $grouped[$key]) {
$grouped[$key] = $value;
}
} else {
$grouped[$key] = $value;
}
}
}
Your new array will look like this (store => price):
[
1 => 402.50,
3 => 235.00
]

PHP Compare two nested arrays to get a new array

I am trying to subtract parts of one nested array from another, but I'm having difficulty specifying the parts that I want to subtract as both values are numbers.
My arrays are, for example:
Array ( [0] => Array ( [id] => 43 [quantity] => 4 ) )
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
And after the subtraction I want the Result to be:
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
I'm using the following code to perform the subtraction, but I can't stop it from subtracting the id from itself:
foreach(array_keys($arrayA) as $id)
{
foreach(array_keys($arrayA[$id]) as $type)
{
$newArray[$id][$type] = $arrayA[$id][$type] - $arrayB[$id][$type];
}
}
print_r($newArray);
Could someone please tell me how I can just effect the [quantity] part of the array, without changing the [id]? With the code as it is I get:
Array ( [0] => Array ( [id] => 0 [quantity] => 2 ) )
Thanks in advance.
$ar1 = array(0 => array('id' => 43, 'quantity' => 4));
$ar2 = array(0 => array('id' => 43, 'quantity' => 2));
$new_array = array();
foreach($ar1 as $key => $value)
{
$new_array[$key] = array('id' => $value['id'], 'quantity' => ($value['quantity'] - $ar2[$key]['quantity']));
}
Array
(
[0] => Array
(
[id] => 43
[quantity] => 2
)
)

Categories