implode for multidimensional array - php

//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

Related

merge two array with loop and key name

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
)
)
)

Insert column value into array using array mapping

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);
}

Split multidimensional array into single array

I have this kind of an array:-
$result=Array([city] => xyz [name] => Array( [0] => Xyz1 [1] => xyz2 ) [email] => Array ( [0] => xyz1#gmail.com [1] => xyz2#gmail.com ) [phone] => Array ( [0] => 23423423-1 [1] => 23423423-2 ) [address] => Array ( [0] => xyz1 [1] => xyz2 ) [dis] => xyz);
and i want this like
$r1=Array ( [city] => xyz [name] => xyz1 [email] => xyz1#gmail.com [phone] => 23423423-1 [address] => xyz1 [dis] => xyz );
$r2=Array ([city] => xyz [name] => xyz2 [email] => xyz2#gmail.com [phone] => 23423423-2 [address] => xyz2 [city] => xyz );
This assumes that you will have more than one array to process and simply sorts your elements into a new, formatted array.
<?php
$results = array(
array(
'city' => 'xyz',
'name' => array( 'Xyz1', 'xyz2' ),
'email' => array( 'xyz1#gmail.com', 'xyz2#gmail.com' ),
'phone' => array( '23423423-1', '23423423-2' ),
'address' => array( 'xyz1', 'xyz2' ),
'dis' => 'xyz'
)
);
$formatted_results = array();
foreach( $results as $result ) {
foreach( array(0,1) as $key ) {
$formatted_results[][$key] = array(
'city' => $result['city'],
'name' => $result['name'][$key],
'email' => $result['email'][$key],
'phone' => $result['phone'][$key],
'address' => $result['address'][$key],
'dis' => $result['dis']
);
}
}
echo "<pre>";
print_r($formatted_results);
This will output
Array
(
[0] => Array
(
[0] => Array
(
[city] => xyz
[name] => Xyz1
[email] => xyz1#gmail.com
[phone] => 23423423-1
[address] => xyz1
[dis] => xyz
)
)
[1] => Array
(
[1] => Array
(
[city] => xyz
[name] => xyz2
[email] => xyz2#gmail.com
[phone] => 23423423-2
[address] => xyz2
[dis] => xyz
)
)
)
You can then access your new values like this:
foreach( $formatted_results as $value ) {
$r1 = $value[0];
$r2 = $value[1];
}
Or if you only have the single array:
$r1 = $formatted_results[0][0];
$r2 = $formatted_results[0][1];

how to reorder array php?

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);

How to Merge Arrays with WHERE Clause?

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);

Categories