Add more values to same key using foreach - php

So I have array like this one:
[
(int) 0 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20014727
],
(int) 1 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027167
],
(int) 2 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027168
],
(int) 3 => [
'user_id' => (int) 20098238,
'location_id' => (int) 20027169
],
(int) 4 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20012490
],
(int) 5 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20016161
],
(int) 6 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20018679
],
(int) 7 => [
'user_id' => (int) 20011189,
'location_id' => (int) 20023569
],
(int) 8 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20008315
],
(int) 9 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20008689
],
(int) 10 => [
'user_id' => (int) 20102015,
'location_id' => (int) 20021267
]
]
I want to get this as restult array like this one
[
(int) 20098238 => [
(int) 0 => (int) 20014727,
(int) 1 => (int) 20027167,
(int) 2 => (int) 20027168,
(int) 3 => (int) 20027169
],
(int) 20011189 => [
(int) 0 => (int) 20012490,
(int) 1 => (int) 20016161,
(int) 2 => (int) 20018679,
(int) 3 => (int) 20023569
],
(int) 20102015 => [
(int) 0 => (int) 20008315,
(int) 1 => (int) 20008689,
(int) 2 => (int) 20021267
]
]
Here is my code, but I am getting only part of sorting
$customerData = [];
foreach ($userIds as $data) {
$i = 0;
foreach ($customerData as $k => $check) {
if ($k == $data['user_id']) {
$customerData[$k][$i] = $data['location_id'];
}
$i++;
}
$customerData[$data['user_id']][$i] = $data['location_id'];
}
As result this code make key like user_id but there is problem with pushing array for that key. I am getting always maximal 2 locations per key.
Thank you for helping!

Your Perfect Solution is here :
$customerData = [];
foreach ($userIds as $data) {
foreach ($customerData as $k => $check) {
if ($k == $data['user_id'] && $data['location_id'] == $check) {
$customerData[$k][] = $data['location_id'];
}
}
$customerData[$data['user_id']][] = $data['location_id'];
}
Live Demo

Simply remove the $i usage and use $customerData[$k][] in your loop. This will add a new occurance to the userid array automatically and as many as exist.
$customerData = [];
foreach ($userIds as $data) {
foreach ($customerData as $k => $check) {
$customerData[$k][] = $data['location_id'];
}
}

Related

Divide values from 2 arrays

I have this array
$a = [
0 => [
'period' => '2017/2018',
'product' => 'AM',
'quantity_1' => 20,
'quantity' => 25,
],
1 => [
'period' => '2018/2019',
'product' => 'AM',
'quantity_1' => 12,
'quantity' => 19,
],
2 => [
'period' => '2017/2018',
'product' => 'DC',
'quantity_1' => 20,
'quantity' => 25,
],
3 => [
'period' => '2018/2019',
'product' => 'DC',
'quantity_1' => 8,
'quantity' => 10,
]
]
The idea is to divide values by period and by product, in this case we have 2 products 'AM' && 'DC'. So divide values fom 2018/2019 to 2017/2018 for product AM and for product DC. I need to get the result like this :
$result = [
0 => [
'product' => 'AM'
'quantity_1' => 12/20 = 0.6
'quantity_2' => 19/25 = 0.76
],
1 => [
'product' => 'DC'
'quantity_1' => 8/20 = 0.4
'quantity_2' => 10/25 = 0.4
]
]
I tried with foreach but I think exist some other easy ways to do that. If you have some ideas I will appreciate this. Thank you for your time.
I tried like this :
$i = 0;
foreach ($results as $result){
$result[] = [
'product' => $result['product'],
'tonnes_prod' => $results[$i+1]['quantity_1'] / $results[$i]['quantity_1']
];
$i++;
}
But I get the error : #message: "Notice: Undefined offset: 28"
Remake your array first, and then calculate the result
$temp = [];
foreach ($a as $x){
$temp[$x['product']][$x['period']] = $x;
}
$result = [];
foreach ($temp as $key => $res){
$result[] = [
'product' => $key,
'quantity_1' => $res['2018/2019']['quantity_1'] / $res['2017/2018']['quantity_1'],
'quantity_2' => $res['2018/2019']['quantity'] / $res['2017/2018']['quantity'],
];
}
demo

Need to push the key and value inside associative Array?

I need to push the more key and its value inside the array. If I use below code first key pair replaced by 2nd one.
For your Reference:
Code Used:
foreach ($projectData['projectsections'] as $key => $name) {
$projectData['projectsections'][$key] = ['name' => $name];
$projectData['projectsections'][$key]= ['id' => '1'];
}
Current result:
'projectsections' => [
(int) 0 => [
'id' => '1'
],
(int) 1 => [
'id' => '1'
]
],
Expected:
'projectsections' => [
(int) 0 => [
'name' => 'test1',
'id' => '1'
],
(int) 1 => [
'name' => 'test2',
'id' => '1'
]
],
How can I build this array in PHP?? Any one help??
You need to either add the entire array:
$projectData['projectsections'][$key] = ['name' => $name, 'id' => '1'];
Or add with the key name:
$projectData['projectsections'][$key]['name'] = $name;
$projectData['projectsections'][$key]['id'] = '1';
With
$projectData['projectsections'][$key] = ['name' => $name];
$projectData['projectsections'][$key]= ['id' => '1'];
you are setting a new Array for that $key. This is not what you want.
This should work:
$projectData['projectsections'][$key] = ['name' => $name, 'id' => '1'];
Change it to :
foreach ($projectData['projectsections'] as $key => $name) {
$projectData['projectsections'][$key]['name'] = $name;
$projectData['projectsections'][$key]['id'] = '1';
}

Php array transformation and combination

So I have array like this one
[
'custid' => [
'customer_number_1' => '20098374',
'customer_number_8' => '20098037',
'customer_number_15' => '20098297'
],
'destid' => [
'destination_numbers_1' => [
(int) 0 => '20024838',
(int) 1 => '20041339'
],
'destination_numbers_8' => [
(int) 0 => '20008293'
],
'destination_numbers_15' => [
(int) 0 => '20016969',
(int) 1 => '20022919',
(int) 2 => '20025815',
(int) 3 => '20026005',
(int) 4 => '20027083',
(int) 5 => '20045497'
]
]
]
Goal is to merge cust id with destid in pairs and should look like so
[
(int) 0 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20024838'
],
(int) 1 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098374',
'sap_destination_id' => '20041339',
],
(int) 2 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098037',
'sap_destination_id' => '20008293,
],
(int) 3 => [
'user_id' => (int) 1,
'sap_customer_id' => '20098297'
'sap_destination_id' => '20016969',
],
...
I have tried with code below, but I am getting destination_id number as array and duplicated customer numbers. Also I have tried with array walk but result is same.
$data = [];
foreach ($sap_data['custid'] as $custid) {
foreach ($sap_data['destid'] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
Thx for helping!
You should make inner loop in other way
foreach($sap_data['custid'] as $k => $custid) {
// Make destination key
$dkey = str_replace('customer_number', 'destination_numbers', $k);
// And get array, for example, destination_numbers_1 for customer_number_1
foreach ($sap_data['destid'][$dkey] as $destid) {
$data[] = [
'user_id' => 1,
'sap_customer_id' => $custid,
'sap_destination_id' => $destid
];
}
}
demo on eval.in

PHP compare element values and push to array

I need to compare elements in array which have same elment value ex. $array[0]['change'] == $array[1]['change'] and if it true than make array push.
I have array like this
[
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 11
],
(int) 2 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 17
],
(int) 3 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 19
],
(int) 4 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 10
],
(int) 5 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 6 => [
'id' => (int) 8,
'uuid' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 19
],
(int) 7 => [
'id' => (int) 9,
'uuid' => '64e146bf-5d76-483f-992c-274cde1202ce',
'change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b',
'company_id' => (int) 20
]
]
I need to get result like this
[
(int) 0 => [
(int) 0 => [
'id' => (int) 2,
'uuid' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 3,
'uuid' => 'c3f388bc-9efb-4c72-b50e-3b6a9075d919',
'change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2',
'company_id' => (int) 18
],
],
(int) 1 => [
(int) 0 => [
'id' => (int) 4,
'uuid' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
(int) 1 => [
'id' => (int) 5,
'uuid' => '52732822-11a9-4c00-8685-2493b88983f8',
'change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e',
'company_id' => (int) 18
],
],
(int) 2 => [
'id' => (int) 6,
'uuid' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'change' => '9a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
(int) 3 => [
'id' => (int) 7,
'uuid' => '8dbe1a81-d722-4261-9b13-ef0b68cbb759',
'change' => '8a356688-d08b-42ee-b26d-19d76bf6543b',
'company_id' => (int) 18
],
.
.
.
]
I have tried to make 2 arrays one with same values and another with different values, than make merge but the result is not as I want... and I hope so that there is "nicer" solution (maybe more readable logic).
$exchange_array = [];
$item_array = [];
foreach ($order->items as $item) {
foreach ($order->items as $exchange) {
if (
isset($item->change) && isset($exchange->change) &&
$item->change == $exchange->change &&
$item->id != $exchange->id
) {
$exchange_array[] = $exchange;
}
if (
!isset($item->change) && !isset($exchange->change) &&
$item->id != $exchange->id
) {
$item_array[] = $exchange;
}
}
}
$item = array_merge($exchange_array,$item_array);
Thank you!
you may use foreach or array_map with this as follows :
$arrays = [
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'c876e544-eca5-4ce1-8563-ed48ac74ebc2'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => 'fa37e5a4-3b5a-4f7d-915e-4807caa5949e'],
['change' => '9a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '8a356688-d08b-42ee-b26d-19d76bf6543b'],
['change' => '701a896a-7d19-4cdd-80a4-f9ca7042945b'],
['change' => '601a896a-7d19-4cdd-80a4-f9ca7042945b'],
];
$list = [];
foreach ($arrays as $array) {
$list[$array['change']][] = $array;
}
print_r(array_values($list));
// if you would like to use array_map instead
$list = [];
array_map(function ($array) use (&$list) {
$list[$array['change']][] = $array;
}, $arrays);
print_r(array_values($list));
live demo : https://3v4l.org/1JO58
Use the following approach:
$result = [];
foreach ($order->items as $item) {
if (isset($result[$item['change']])) {
// check if nested array is a single item(not multidimensional)
if (isset($result[$item['change']]['change'])) {
$result[$item['change']] = [$result[$item['change']], $item];
} else {
$result[$item['change']][] = $item;
}
} else {
$result[$item['change']] = $item;
}
}
$result = array_values($result);

The correct way to convert php array

I have data array like
$data = [
'name' => [
(int) 0 => '095a108478345cac184f956b1e8dee91a5a89f87bbabd7b3fb4058f577adf.jpg',
(int) 1 => '02059.jpg',
(int) 2 => 'avatar.jpg'
],
'type' => [
(int) 0 => 'image/jpeg',
(int) 1 => 'image/jpeg',
(int) 2 => 'image/jpeg'
],
'tmp_name' => [
(int) 0 => 'C:\xampp\tmp\php17AA.tmp',
(int) 1 => 'C:\xampp\tmp\php17BA.tmp',
(int) 2 => 'C:\xampp\tmp\php17BB.tmp'
],
'error' => [
(int) 0 => (int) 0,
(int) 1 => (int) 0,
(int) 2 => (int) 0
],
'size' => [
(int) 0 => (int) 80542,
(int) 1 => (int) 6532,
(int) 2 => (int) 6879
]
]
And i need convert to array like this
$data = [
(int) 0 => [
'name' => '095a108478345cac184f956b1e8dee91a5a89f87bbabd7b3fb4058f577adf.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17AA.tmp',
'error' => (int) 0,
'size' => (int) 80542
],
(int) 1 => [
'name' => '02059.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17BA.tmp',
'error' => (int) 0,
'size' => (int) 6532
],
(int) 2 => [
'name' => 'avatar.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\php17BB.tmp',
'error' => (int) 0,
'size' => (int) 6879
]
]
I'm looking for the correct way to convert the first php array to the second. Is there any of the PHP array functions provided for these actions. Either is possible with CakePHP hash Array management?
Yes, I can make a few foreach loops and create an array what I need, but I'm not sure if there is a more elegant way.
From PHP 5.5 you can use array_column and array_combine to do it.
$ret = []; $keys = array_keys($data);
for ($i=0; $i<3; $i++) {
$ret[$i] = array_combine($keys, array_column($data, $i));
}
Where 3 is the number of elements of name, type, tmp_name, etc.
Demo.
The same thing for PHP < 5.5
$ret = []; $keys = array_keys($data);
for ($i=0; $i<3; $i++) {
$ret[$i] = array_combine($keys, array_map(function($element) use ($i){
return $element[$i];
}, $data));
}
Demo.
And here an example, for the sake of completeness, with only two foreach:
$ret = [];
foreach($data as $key => $val) {
$i = 0;
foreach ($val as $v) {
$ret[$i][$key] = $v;
$i++;
}
}
Which by the way I would use regardless to other implementations.

Categories