i have array and i need to reorder by another array:
here is my array:
Array
(
[0] => Array
(
[name] => Haroldas
[number] => 444
[address] => g.
[city] => eee
[country] => f
[lastname] => r
)
[1] => Array
(
[name] => Lukas
[number] => 999
[address] => rrr
[city] => tttt
[country] => 3
[lastname] => r
)
)
This is another array with columns number which show me how to order number (columns order can to be another):
Array
(
[lastname] => 4
[name] => 1
[number] => 5
[address] => 3
[city] => 0
[country] => 2
)
i need result like this:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
...
)
This should do the job:
<?php
$order = [
'city' => 0,
'name' => 1,
'country' => 2,
'address' => 3,
'lastname' => 4,
'number' => 5,
];
$data = [
[
'name' => 'Haroldas',
'number' => '444',
'address' => 'g.',
'city' => 'eee',
'country' => 'f',
'lastname' => 'r',
],
[
'name' => 'Lukas',
'number' => '999',
'address' => 'rrr',
'city' => 'ttt',
'country' => 3,
'lastname' => 'r',
],
];
foreach ($data as &$entry) {
uksort($entry, function ($a, $b) use ($order) {
return strcmp($order[$a], $order[$b]);
});
}
print_r($data);
The output is:
Array
(
[0] => Array
(
[city] => eee
[name] => Haroldas
[country] => f
[address] => g.
[lastname] => r
[number] => 444
)
[1] => Array
(
[city] => ttt
[name] => Lukas
[country] => 3
[address] => rrr
[lastname] => r
[number] => 999
)
)
If you want to reorder that use foreach then create a new array.
$arrReorder = [];
foreach($yourArr as $key => $val){
$arrReorder[] = ['city' => $val['city'], 'name' => $val['name'], 'country' => $val['country'], 'address' => $val['address'], 'lastname' => $val['lastname'], 'number' => $val['number'] ];
}
print_r($arrReorder);
Related
i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
I'm trying to merge and calc only by choosen key. For example I have the following array:
$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];
$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];
$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];
Final output should look like this :
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
I tried the following :
$courses = [];
foreach ($array as $key => $item) {
if (!key_exists($key, $courses)) {
$courses[$key] = [];
}
foreach ($item as $row) {
$id = $row['id'];
if (!key_exists($id, $courses[$key])) {
$courses[$key][$id] = $row;
continue;
}
$r = &$courses[$key][$id];
$r['amount'] += $row['amount'];
}
}
Output:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);
I have two array i want to add another key in first array by matching keys with second array below are the array that I have
First Array
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
Second array that I have
Array
(
[0] => Array
(
[id] => 38395
[request_by] => 2
[request_to] => 5198
)
)
Required Result
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[request_sent] => true
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
This is just because requested by user 2 has sent friend request to 5198 user and so that's why request_sent is true to this user only and remining will false if friend request not send I want to do this work using buitl in function instead of loops.
Seems very easy, may be i don't uderstand the task
$tmp = array_flip(array_column($arr2, 'request_to'));
foreach($arr1 as &$v) {
$v['request_sent'] = isset($tmp[$v[id]]);
}
Solution without loops (check it online):
<?php
$users = [
['id' => 5198, 'full_name' => 'Afnan', 'username' => 'eay.d', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5213, 'full_name' => 'Nouf', 'username' => 'noufalswailem', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5218, 'full_name' => 'Mohammed Bin Abdullah', 'username' => 'almuribadh', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5225, 'full_name' => 'Shadin Alshobaily', 'username' => 'shash', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5251, 'full_name' => 'Razan', 'username' => 'ra.m', 'is_live' => 1, 'picture' => 'someurl'],
];
$requests = [
['request_by' => 2, 'request_to' => 5198],
['request_by' => 2, 'request_to' => 5225],
];
array_walk($users, 'setUserRequestStatus', array_column($requests, 'request_to'));
var_dump($users);
function setUserRequestStatus(&$user, $key, $requests_sent_to) {
$user['request_sent'] = in_array($user['id'], $requests_sent_to);
}
//for below array i need the values of name to be semi-colon separated
output should be
monica;pradnesh
Array
(
[0] => Array
(
[0] => Array
(
[name] => Monica
[address] => Surat
[mobile_number] => 8956231245
[telephone_number] =>
[email_id] => monica#kritva.com
[DOB] => 0000-00-00
[gender] => female
[PAN_number] => ASDFG4567A
[customer_type] => SALES
[dependency_type] => Retail
[cust_id] => 9055954
[state] => Gujarat
[city] => Surat
[zipcode] => 752852
[exist_from] => 2016-12-20
[edit_date] => 0000-00-00
[staff_id] =>
[BA_id] =>
[id] => 31
[nationality] => Indian
)
)
[1] => Array
(
[0] => Array
(
[name] => Pradnesh
[address] => Surat
[mobile_number] => 8956231245
[telephone_number] =>
[email_id] => pradnesh.valapkar#kritva.com
[DOB] => 0000-00-00
[gender] => male
[PAN_number] => GHJKL9876S
[customer_type] => NRI
[dependency_type] => BA
[cust_id] => 2736738
[state] => Gujarat
[city] => Surat
[zipcode] => 895623
[exist_from] => 2016-12-21
[edit_date] => 0000-00-00
[staff_id] =>
[BA_id] => 5822043
[id] => 33
[nationality] => Indian
)
)
)
first extract all the name to array $names, then use implode like this:
$names = array_map(function($v){return $v[0]['name'];}, $array);
implode(',', $names);
Might be this can show you direction.
<?php
$data = array(
array(
array(
'name' => 'Monica',
'address' => 'surat'
),
array(
'name' => 'Priya',
'address' => 'surat'
)
),
array(
array(
'name' => 'Pradnesh',
'address' => 'surat'
),
array(
'name' => 'test',
'address' => 'surat'
)
)
);
array_map(function ($entry) {
echo implode(';',array_map(function ($d) {
return $d['name'];
}, $entry));
}, $data);
?>
Output
Monica;PriyaPradnesh;test
How to add 2nd array into 1st array where [ myid ] matches.
1st Array
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
)
)
2nd Array
Need these projects to be added into Array 1
Array
(
[0] => Array
(
[myid] => 70
[projects] => 20
)
[1] => Array
(
[myid] => 75
[projects] => 43
)
)
I have tried to Merge Array's but no use, i am getting 2 more objects in to this array
I hope my requirement is clear and readable, if not please mention in comment so i could explain it more.
The answer can be in codeigniter also i am using Codeigniter framework
assume that you want the result array to be merging the projects to your first array
you want the result set to be like this
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
[projects] => 20
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
[projects] => 43
)
)
so try this code
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]->myid == $val->myid){
$a[$key]->projects = $val->projects;
}
}
if converted the stdArray to Array
json_decode($a,TRUE)
and json_decode($b, TRUE)
$a = Array
(
0 => Array
(
'myid' => 70,
'realname' => 'Kishore',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => 'Yes',
'designation' => 'Mechanical',
'address' => 'Dwarakanagar 5th lane',
'city' => 'Vishakhapatnam',
'email' => 'yesapps.india#gmail.com',
),
1 => Array
(
'myid' => 75,
'realname' => 'Vinod kumar',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => '',
'designation' => '',
'address' => '',
'city' => '',
'email' => 'vinod.k.alluri#gmail.com'
)
);
$b = Array ( 0 => Array(
'myid' => 70,
'projects' => 20
),
1 => Array(
'myid' => 75,
'projects' => 43
)
);
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]['myid'] == $val['myid']){
$a[$key]['projects'] = $val['projects'];
}
}
print_r($a);
NOTE:
$b is the 2nd array
$a is the 1st array
foreach($array1 as $key => $array){
foreach($array as $key2 => $value){
$results[$key]['myid'] = $value;
}
}
foreach($array2 as $key => $array){
foreach($array as $key3 => $value){
$results[$key3]['projects'] = $value;
}
}
print_r($results);