I want to combine two arrays of objects. Let me give you an example:
Example:
// First array:
$array1 = [
{ name => 'Joe', p_id => 1 },
{ name => 'Bob', p_id => 2 },
{ name => 'Sam', p_id => 4 }
]
// Second array:
$array2 = [
{ id => 1, name => 'X' },
{ id => 2, name => 'Y' },
{ id => 4, name => 'Z' }
]
Expected output:
$output = [
{ name => 'Joe + X', id => 1 },
{ name => 'Bob + Y', id => 2 },
{ name => 'Sam + Z', id => 4 }
]
Goal:
I want the fastest possible way to combine the name property in the second array with the name property in the first array.
Note: The p_id property in the first array is the same as the id property in the second array.
What i try:
I've used nested loops that have a very low speed.
array_map is the solution!
Given:
$first = [
{ name => 'Joe', p_id => 1 },
{ name => 'Bob', p_id => 2 },
{ name => 'Sam', p_id => 4 },
];
$second = [
{ id => 1, name => 'X' },
{ id => 2, name => 'Y' },
{ id => 4, name => 'Z' },
];
The solution is just simply:
$result = array_map(
static function (\stdClass $first, \stdClass $second): array {
return [
'name' => $first->name . ' + ' . $second->name,
'id' => $first->p_id,
];
},
$first, $second
);
PS: I assume the objects are \stdClass, replace it by the correct one.
Here is a solution for when the ids of the elements inside the array are not in order. Notice that I have changed the order of $array1. Just a bit better than the regular nested loops, on the loop of the $array2 it will remove the "found" elements to improve speed of the next loop.
From:
// First array:
$array1 = [
(object) ['name' => 'Joe', 'p_id' => 1],
(object) ['name' => 'Sam', 'p_id' => 4],
(object) ['name' => 'Bob', 'p_id' => 2],
];
// Second array:
$array2 = [
(object) ['id' => 1, 'name' => 'X'],
(object) ['id' => 2, 'name' => 'Y'],
(object) ['id' => 4, 'name' => 'Z'],
];
Solution:
$result = [];
foreach ($array1 as $array1Element) {
for ($i=0;$i<count($array2);$i++) {
if ($array1Element->p_id === $array2[$i]->id) {
$array2[$i]->name = $array1Element->name . ' + ' . $array2[$i]->name;
$result[] = $array2[$i];
unset($array2[$i]);
$array2 = array_values($array2);
break;
}
}
}
Related
I have a a number of values/IDs that need to be translated to a single ID, what is the recommended method using PHP?
For example, I want IDs 38332, 84371, 37939, 1275 to all translate to ID 1234 and IDs222, 47391, 798 to all translate to ID 1235, etc. .
I'm thinking PHP has something built-in to handle this efficiently?
I'm thinking PHP has something built-in to handle this efficiently?
You can use the standard array as a map, quickly translating one ID to another:
$table[38332]; # int(1234)
depending on how you store your overall translation table, you can create a function that returns the translation from its input:
$table = $translation('I want IDs 38332, 84371, 37939, 1275 to all translate to ID 1234');
$result = $table[1275] ?? null; # int(1234)
Example:
$parseId = static fn(string $i) => (int)trim($i);
$translation = static fn(string $buffer): array
=> preg_match_all('~((?:\d+,\s*)+\d+)\s+to all translate to ID\s*(\d+)~', $buffer, $_, PREG_SET_ORDER)
? array_reduce($_, static fn (array $carry, array $item): array => [
$ids = array_map($parseId, explode(',', $item[1])),
$carry += array_fill_keys($ids, $parseId($item[2])),
$carry,][2], []) : [];
This is pretty easy to accomplish with PHP, here's one way you could do it:
Using this method, you populate the $map array, using the id you want to replace with as the key, and the value being an array of the keys you want to be replaced. It then calculates a simple key => value array based on this to make comparison a lot quicker.
Instead of creating a copy of the data, you could use foreach ($data as &$record)
$data = [
[
'id' => 1,
'foreign_id' => 38332,
'text' => 'a'
],
[
'id' => 2,
'foreign_id' => 84371,
'text' => 'b'
],
[
'id' => 3,
'foreign_id' => 37939,
'text' => 'c'
],
[
'id' => 4,
'foreign_id' => 1275,
'text' => 'd'
],
[
'id' => 5,
'foreign_id' => 222,
'text' => 'e'
],
[
'id' => 5,
'foreign_id' => 47391,
'text' => 'f'
],
[
'id' => 5,
'foreign_id' => 798,
'text' => 'g'
]
];
$map = [
123 => [
38332,
84371,
37939,
1275
],
1235 => [
222,
47391,
798
]
];
// Calculate a map to speed things up later
$map_calc = [];
foreach ($map as $destination_id => $ids) {
foreach ($ids as $id) {
$map_calc[$id] = $destination_id;
}
}
$new_data = [];
foreach ($data as $record) {
if (isset($map_calc[$record['foreign_id']]))
$record['foreign_id'] = $map_calc[$record['foreign_id']];
$new_data[] = $record;
}
var_dump($new_data);
I have an array in which i want to combine the common fields together.
So that the common names get grouped together and they contain the common values of that particular name.
Below is my array
[
[
count_employee: 2,
name: "Harry"
],
[
count_employee: 61,
name: "Jack"
],
[
count_employee: 11,
name: "John"
],
[
count_warehouse: 1,
name: "Harry"
],
[
count_warehouse: 77,
name: "John"
],
[
count_warehouse: 45,
name: "Jack"
]
]
I want the output to be
[
[
name: "Harry",
count_employee:2
count_warehouse:1
],
[
name: "Jack",
count_employee: 61
count_warehouse: 45
],
[
name: "John",
count_employee:11
count_warehouse:77
],
]
So far i have tried this out
foreach ($data as $key => $value) {
$group[$value['name']]['name'] = $value['name'];
$group[$value['name']][$key]['count_employee'] = $value['count_employee'];
$group[$value['name']][$key]['count_warehouse'] = $value['count_warehouse'];
}
Loop over the array, and use the name as the index of each element. If no element by that index does not exist, define it as an empty array.
Then all you need to do is add the fields (if they exist) to the proper column.
Your issue was that you were creating an array with one too many dimensions, instead of adding each value to the current count - in addition to the fact that you will not always have both the warehouse and employee count defined for each iteration.
$result = [];
foreach ($arr as $v) {
// Initialize the person
if (!isset($result[$v['name']])) {
$result[$v['name']] = ["name" => $v['name'], "count_employee" => 0, "count_warehouse" => 0];
}
// Add values
if (isset($v['count_employee']))
$result[$v['name']]['count_employee'] += $v['count_employee'];
if (isset($v['count_warehouse']))
$result[$v['name']]['count_warehouse'] += $v['count_warehouse'];
}
// print_r($result);
Live demo at https://3v4l.org/TeNKF
You can use array_walk with array_key_exists
$res=[];
array_walk($arr, function($v,$k) use (&$res){
if(array_key_exists($v['name'], $res))
$res[$v['name']]['count_warehouse'] = $v['count_warehouse'];
else
$res[$v['name']] = $v;
});
print_r(array_values($res));
Live Demo
Assuming all the array has a name key for each sub array contained within, we can use the name as a key for an array and merge arrays with corresponding name values.
<?php
$data=
[
[
'count_employee'=> 2,
'name'=> 'Harry'
],
[
'count_employee'=> 61,
'name'=> 'Jack'
],
[
'count_employee'=> 11,
'name'=> 'John'
],
[
'count_warehouse'=> 1,
'name'=> 'Harry'
],
[
'count_warehouse'=> 77,
'name'=> 'John'
],
[
'count_warehouse'=> 45,
'name'=> 'Jack'
]
];
$output = [];
foreach($data as $item) {
$output[$item['name']] = array_merge($item, $output[$item['name']] ?? []);
}
var_export($output);
Output:
array (
'Harry' =>
array (
'count_warehouse' => 1,
'name' => 'Harry',
'count_employee' => 2,
),
'Jack' =>
array (
'count_warehouse' => 45,
'name' => 'Jack',
'count_employee' => 61,
),
'John' =>
array (
'count_warehouse' => 77,
'name' => 'John',
'count_employee' => 11,
),
)
I need to add new elemets to my array when a new category value is encountered. When a category value is encountered after the first time, its value1 and value2 values should be added to the first encounter's respective values.
Also, in the result array, I no longer wish to keep the category column. The category-grouping rows should use the category value as its name value.
Sample input:
$datas = [
[
'category' => 'Solution',
'name' => 'Name1',
'value1' => 20,
'value2' => 21
],
[
'category' => 'Solution',
'name' => 'Name2',
'value1' => 30,
'value2' => 31
],
[
'category' => 'Solution1',
'name' => 'Name3',
'value1' => 40,
'value2' => 41
]
];
Desired result:
[
['name' => 'Solution', 'value1' => 50, 'value2' => 52],
['name' => 'Name1', 'value1' => 20, 'value2' => 21],
['name' => 'Name2', 'value1' => 30, 'value2' => 31],
['name' => 'Solution1', 'value1' => 40, 'value2' => 41],
['name' => 'Name3', 'value1' => 40, 'value2' => 41]
]
I tried like this:
private function groupByProductSuperCategory($datas)
{
$return = [];
foreach ($datas as $data) {
$return[$data['category']][$data['name']] = array_sum(array_column('category', $data);
}
return $return;
}
The idea is to calculate first all sum values for by category, and after that just put values from name like another array. Have you an idea of how to do that?
From the posted array... To end in the desired array, there is some tiny fixes to do first. But I assumed it was due to typos while copying here...
So here is the array I started with:
$result = [
0 => [
"category" => 'Solution',
"name" => 'Name1',
"value1" => 20,
"value2" => 21
],
1 => [
"category" => 'Solution',
"name" => 'Name2',
"value1" => 30,
"value2" => 31
],
2 => [
"category" => 'Solution1',
"name" => 'Name3',
"value1" => 40,
"value2" => 41
]
];
Now, that re-organization of the data is a bit more complex than it looks... You need to perform several loops to:
Find distinct "category" names
Perform the summations for each
Add the sum item and the single items
So here is the code I ended with:
function groupByProductSuperCategory($datas){
$category = [];
$return = [];
// Find distinct categories
foreach ($datas as $data) {
if(!in_array($data["category"],$category)){
array_push($category,$data["category"]);
}
}
// For each distinct category, add the sum item and the single items
foreach ($category as $cat) {
// Get the sums
if(!in_array($cat,$return)){
$sum1 = 0;
$sum2 = 0;
foreach ($datas as $data) {
if($data["category"] == $cat){
$sum1 += $data["value1"];
$sum2 += $data["value2"];
}
}
}
// Push the sums in the return array
array_push($return,[
"name" => $cat,
"value1" => $sum1,
"value2" => $sum2,
]);
// Push the single elements
foreach ($datas as $data) {
if($cat == $data["category"]){
array_push($return,[
"name" => $data["name"],
"value1" => $data["value1"],
"value2" => $data["value2"],
]);
}
}
}
return $return;
}
Here is a PHPFiddle to try it out... Hit [F9] to run.
It is much more direct, efficient, and readable to implement a single loop and push reference variables into the result array to allow summing based on shared categories without keeping track of the actual indexes of the category rows.
Code: (Demo)
$result = [];
foreach ($array as $row) {
if (!isset($ref[$row['category']])) {
$ref[$row['category']] = [
'name' => $row['category'],
'value1' => 0,
'value2' => 0
];
$result[] = &$ref[$row['category']];
}
$ref[$row['category']]['value1'] += $row['value1'];
$ref[$row['category']]['value2'] += $row['value2'];
unset($row['category']);
$result[] = $row;
}
var_export($result);
I've got this PHP array:
<?php
$cities = [
'amsterdam' => $amsterdam,
'prague' => $prague,
'lisboa' => $lisboa
];
$amsterdam = [
65 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 100
]
],
173 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 2500
]
],
...
];
$prague = [
132 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 2100
]
],
956 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 2500
]
],
...
];
$lisboa = [
175 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'boo',
'price' => 6500
]
],
64 => [
'table' => Object
'something' => false,
'data' => [
'foo' => 'hoo',
'price' => 20
]
],
...
];
?>
and I need to sort it by the subarray value ['data']['price'] so the output is like this:
<?php
$cheapest_cities [
'lisboa' => $lisboa, // because 64->data->price is 20
'amsterdam' => $amsterdam, // beacuse 65->data->price is 100
'prague' => $prague // bacause 132->data->price is 2100
];
?>
I tried several usort combinations, but the problem is, that i never know what the subarray index will be (65, 173, 132, 956, 175, 64) in my example.
Do you have any idea how to sort it?
The data comes from database:
<?php
$amsterdam = $this->getTable()->where(['package_id' => [1,2,3]])->order('package_id')->fetchPairs('id');
$lisboa = $this->getTable()->where(['package_id' => [4,5]])->order('package_id')->fetchPairs('id');
$prague = $this->getTable()->where(['package_id' => [6]])->order('package_id')->fetchPairs('id');
return [
'amsterdam' => $amsterdam,
'lisboa' => $lisboa,
'prague' => $prague,
];
?>
Thank you
I would start by making a new array, which has the smallest price of every city as value
For this I use an array_map function which reduces the $items to the price with array_reduce
$map_prices = function($n) {
$reduce_smallest_price = function($carry, $item) {
return $item['data']['price'] < $carry
? $item['data']['price']
: $carry;
};
return array_reduce($n, $reduce_smallest_price, INF);
};
$cities_price = array_map($map_prices, $cities);
asort($cities_price);
I use this prices array to sort the original array with uksort
uksort($cities, function($a, $b) {
global $cities_price;
return strnatcmp($cities_price[$a], $cities_price[$b]);
});
Here is a live example on 3v4l: https://3v4l.org/8B9VN
Don't use usort as it will remove your keys. Use uasort.
Just a quick idea:
Inside the callback function of uasort you could search the minimum of your item e.g. via array_reduce.
array_reduce($city, function($carry, $item) {
return $carry === 0 ? $item['data']['price'] : min($carry, $item['data']['price']);
}, 0);
This snippet gets the minimum of a city array. Then it should be easy to compare the values.
Full example:
function getMinimumPrice($cityArray) {
return array_reduce($cityArray, function($carry, $item) {
return $carry === 0 ? $item['data']['price'] : min($carry, $item['data']['price']);
}, 0);
}
uasort($cities, function($city1, $city2) {
$priceCity1 = getMinimumPrice($city1);
$priceCity2 = getMinimumPrice($city2);
return $priceCity1 <=> $priceCity2;
});
I want to know that is there a way to insert certain elements of an array into a new array. I mean I have an array containing 10 objects. Each object has 3 or four fields for example id, name , age , username. now I want to insert the id's of all the objects into the new array with a single call.Is there anyway to do that.
$array = [
[0] => [
id =>
name =>
],
[1] = > [
id =>
name =>
]
]
and so on now I want to insert all the id's of all the object into a new array with a single call. Is there a way to do that?
Use array_map() function.
Here is your solution:-
$ids = array_map( function( $arr ){
return $arr["id"];
}, $arr );
echo '<pre>'; print_r($ids);
A basic foreach loop will do the job just fine
$firstArray = array(
array(
'id' => 1,
'name' => 'abc'
),
array(
'id' => 2,
'name' => 'def'
),
array(
'id' => 3,
'name' => 'gh'
)
);
$onlyIds = array();
$onlyKeys = array();
//To get the array value 'id'
foreach($firstArray as $value){
$onlyIds[] = $value['id'];
}
//To get the array keys
foreach($firstArray as $key => $value){
$onlyKeys[] = $key;
}
You could use array_walk which could be considered a "single call"
$array = array(0 => array('id', 'name', 'age'), 1 => array('id', 'name', 'age'));
array_walk($array, function($item, $key) {
// $key is 0 or 1
// $item is either id, name, age
});
You can use array_column.
$arr = [ ['id' => 1, 'username' => 'a'], ['id' => 2, 'username' => 'b'] ];
$ids = array_column($arr, 'id')
$ids == [1, 2]