I try to merge the data's in the arrays in 'c' and 'a' inside MYDATA but is it possible? Some examples and tips would be helpful! I would love to hear from you!
[0] => Array
(
[MYDATA] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
)
[c] => Array
(
[my_test] => math
)
[a] => Array
(
[my_date] => 2017-08-13
)
)
I would want the result to be like
[MYDATA] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
[my_test] => math
[my_date] => 2017-08-13
Just merge the elements one by one:
<?php
$input = [
'MYDATA' => [
'id' => 79,
'my_birth_day' => '1990-06-20',
'my_address' => 400,
'my_age' => 26,
'my_name' => 'Joy',
'my_id' => 1,
'created' => '2017-06-19 15:39:44'
],
'c' => [
'my_test' => 'math'
],
'a' => [
'my_date' => '2017-08-13'
]
];
$output = [];
array_walk($input, function($element) use (&$output) {
$output = array_merge($output, $element);
});
print_r($output);
The order in which you iterate over the input array determines which entries will win in case you have key collisions.
The output obviously is:
Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
[my_test] => math
[my_date] => 2017-08-13
)
The function needed is array_merge(), it takes an unlimited number of parameters, the order of the parameters dictates the order in which the arrays are merged, in this case it would not matter, but say you had matching keys, say two 'my_address' keys the value from the last array defining this key will be returned as a result as the function call.
See the docs here: http://php.net/manual/en/function.array-merge.php
Merge the inner arrays, assuming your outermost array is called $array you can try the following:
<?php
$array = array(
'MYDATA' => array(
'id' => '79',
'my_birth_day' => '1990-06-20',
'my_address' => '400',
'my_age' => '26',
'my_name' => 'Joy',
'my_id' => '1',
'created' => '2017-06-19 15:39:44'
),
'c' => array(
'my_test' => 'math'
),
'a' => array(
'my_date' => '2017-08-13'
)
);
$mergedMyData = array_merge($array['MYDATA'], $array['c'], $array['a']);
//output the data
print_r($mergedMyData);
?>
hope this helps
Related
I'm sorry for not using the correct terminology. I'm very much a 'weekend warrior' when it comes to programming, but trying to get better.
I have two indexed arrays with associative arrays as the values. ArrayOne has a value for [uid_apps] that I want to use as a filter for ArrayTwo so I can create ArrayThree. The thrid array will only include array items from ArrayTwo that match the [uid_apps] value in ArrayOne.
I looked ar array_combine() and array_intersect(), but I didn't see a clear path to success. I also messed around with array_filter(), but wasn't able to make it work.
Below are samples of arrayOne, arrayTwo, and the desired arrayThree. Any help you can offer is greatly appreciated.
ArrayOne
[0] => Array
(
[uid_appMembership] => 3
[uid_apps] => 1
[uid_main] => 3
[privileges] => 555
)
[1] => Array
(
[uid_appMembership] => 4
[uid_apps] => 3
[uid_main] => 3
[privileges] => 555
)
ArrayTwo
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 2
[name_apps] => CTE Work Based Learning Solution
[site_apps] => do_000
[team_apps] => do_cte
[admin_uid] => 3
[dir_apps] => do_cte-wbl
)
[2] => Array
(
[uid_apps] => 3
[name_apps] => GHS Parking Permit Solution
[site_apps] => ghs_001
[team_apps] => ghs_parking
[admin_uid] => 3
[dir_apps] => ghs_parking-permits
)
[3] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
Desired ArrayThree I'd like to use arrayOne and arrayTwo to create this array.
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
loop through array and check uid_apps value is available in other array or not if available than add it in third array.check using in array.
$arr1 = array(
array(
'uid_appMembership' => 3,
'uid_apps' => 1,
'uid_main' => 3,
'privileges' => 555
),
array(
'uid_appMembership' => 4,
'uid_apps' => 3,
'uid_main' => 3,
'privileges' => 555
)
);
$arr2 = array(
array(
'uid_apps' => 1,
'name_apps' => 'GHS Walk Through Evaluation',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_admin',
'admin_uid' => 2,
'dir_apps' => 'ghs_walk-through-evaluation'
),array(
'uid_apps' => 2,
'name_apps' => 'CTE Work Based Learning Solution',
'site_apps' => 'do_000',
'team_apps' => 'do_cte',
'admin_uid' => 3,
'dir_apps' => 'do_cte-wbl'
),array(
'uid_apps' => 3,
'name_apps' => 'GHS Parking Permit Solution' ,
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_parking',
'admin_uid' => 3,
'dir_apps' => 'ghs_parking-permits'
),array(
'uid_apps' => 4,
'name_apps' => 'GHS F-List',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_counseling',
'admin_uid' => 3,
'dir_apps' => 'ghs_flist'
)
);
$arr3 = array();
foreach ($arr2 as $key => $value) {
$res = chk_val($arr1,$value['uid_apps']);
if($res == true){
array_push($arr3,$arr2[$key]);
}
}
function chk_val($arr,$val){
foreach ($arr as $key => $value) {
if(in_array($val,$value)){
return true;
}else{
return false;
}
}
}
Working example : http://phpfiddle.org/main/code/sdri-fbpk
here is how you can get desired array
$dataa = array();
foreach ($names as $key => $name) {
foreach($ips as $key2=>$ip){
if($name['uid_apps'] == $ip['uid_apps']){
$dataa[] = $name;
}
}
}
print_r($dataa);
This question already has answers here:
Merging two multidimensional arrays on specific key [duplicate]
(7 answers)
Closed 4 years ago.
I got two arrays that I would like to combine based on "client_id" key (using PHP function is preferable) :
[all_client] => Array
(
[0] => Array
(
[client_id] => 1
[client_name] => Thomas Berg
[client_phone] => 12313123
[client_email] => aaaa#aaa.com
)
[1] => Array
(
[client_id] => 2
[client_name] => John Doe
[client_phone] => 4231241
[client_email] => asdas#asdas.com
)
)
[all_client_document] => Array
(
[0] => Array
(
[client_document_id] => 3
[client_document_number] => BX100
[client_document_type] => passport
[client_document_issued_date] => 2018-10-17
[client_document_expired_date] => 2018-12-02
[client_id] => 1
)
[1] => Array
(
[client_document_id] => 4
[client_document_number] => DJ200
[client_document_type] => passport
[client_document_issued_date] => 2018-10-15
[client_document_expired_date] => 2018-11-23
[client_id] => 2
)
)
)
How to merge these two array? Any PHP function to achieve this without foreach looping? I would hope to see the result as below :
[new_result] => Array
(
[0] => Array
(
[client_id] => 1
[client_name] => Thomas Berg
[client_phone] => 12313123
[client_email] => aaaa#aaa.com
[client_document_id] => 3
[client_document_number] => BX100
[client_document_type] => passport
[client_document_issued_date] => 2018-10-17
[client_document_expired_date] => 2018-12-02
)
[1] => Array
(
[client_id] => 2
[client_name] => John Doe
[client_phone] => 4231241
[client_email] => asdas#asdas.com
[client_document_id] => 4
[client_document_number] => DJ200
[client_document_type] => passport
[client_document_issued_date] => 2018-10-15
[client_document_expired_date] => 2018-11-23
)
)
How can I achieve this? Any help is much appreciated.
I found the answer based on the same question :
$first = array_column($data['all_client'], null, 'client_id');
$second = array_column($data['all_client_document'], null, 'client_id');
$result = array_values(array_replace_recursive($first, $second));
It reproduces as what I want also and simpler, thanks anyone!
You can achieve this by using array_map() function:
function make_my_data($a, $b)
{
return array_merge($a,$b);
}
$a = [
0 => [
'user_id' => 1,
'user_name' => "Ram",
],
1 => [
'user_id' => 2,
'user_name' => "Raj",
]
];
$b = [
0 => [
'user_id' => 1,
'user_emai' => "ram#abc.com",
],
1 => [
'user_id' => 2,
'user_email' => "raj#abc.com",
]
];
$d = array_map("make_my_data", $a , $b);
echo "<pre>"; print_r($d);
Output:
Array
(
[0] => Array
(
[user_id] => 1
[user_name] => Ram
[user_emai] => ram#abc.com
)
[1] => Array
(
[user_id] => 2
[user_name] => Raj
[user_email] => raj#abc.com
)
)
The following is my result array
Array (
[0] => Array
(
[ProductID] => 220
[TextID] => 477
[ProductName] => Hugo Woman
[Price] => 43.91
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg
[CategoryID] => 1
[SubCategoryID] => 1
[View] => 0
)
[1] => Array
(
[ProductID] => 616
[TextID] => 959
[ProductName] => Hugo XY
[Price] => 44.95
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
[2] => Array
(
[ProductID] => 650
[TextID] => 991
[ProductName] => Hugo Just Different
[Price] => 45.76
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
)
I have a second array with subcategories, in which the key is referencing to the SubCategoryID:
Array
(
[1] => Array
(
[EN] => Ladies
[NL] => Dames
)
[2] => Array
(
[EN] => Men
[NL] => Heren
)
)
I want to loop through the result array and remove the keys who don't have a SubCategoryID listed in the second array. I looked at http://php.net/manual/en/function.array-filter.php, but can't figure out the best way to do this.
Thank you!
There are two solutions to the above problem, one with using simple for loop and one with using array_walk() function.
Here, $result_array is your result array and $subcategory_array is your subcategory array.
Solution(1):
$subcategory_ids = array_keys($subcategory_array);
$arrLength = count($result_array);
for($i = 0; $i < $arrLength; ++$i){
if(!in_array($result_array[$i]['SubCategoryID'], $subcategory_ids)){
unset($result_array[$i]);
}
}
// display $result_array
echo "<pre>"; print_r($result_array);
Solution(2):
$subcategory_ids = array_keys($subcategory_array);
function filter_arr($item, $key){
global $result_array, $subcategory_ids;
if(!in_array($item['SubCategoryID'], $subcategory_ids)){
unset($result_array[$key]);
}
}
array_walk($result_array, "filter_arr");
// display $result_array
echo "<pre>"; print_r($result_array);
Please try i thnik this help to you..
$array = array (
0 => array
(
'ProductID' => 220,
'TextID' => 477,
'ProductName' => 'Hugo Woman',
'Price' => 43.91,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg',
'CategoryID' => 1,
'SubCategoryID' => 1,
'View' => 0
),
1 => array
(
'ProductID' => 616,
'TextID' => 959,
'ProductName' => 'Hugo XY',
'Price' => 44.95,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg',
'CategoryID' => 1,
'SubCategoryID' => 2,
'View' => 0
),
'2' => array
(
'ProductID' => 650,
'TextID' => 991,
'ProductName' => 'Hugo Just Different',
'Price' => 45.76,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg',
'CategoryID' => 2,
'SubCategoryID' => 1,
'View' => 0
),);
$array1 = array (
1 => array
(
'EN' => 'Ladies',
'NL' => 'Dames'
),
2 => array
(
'EN' => 'Men',
'NL' => 'Heren'
),);
foreach($array as $newArray){
if (array_key_exists($newArray['SubCategoryID'], $array1)) {
echo '<pre>';
print_r($newArray);
echo '</pre>';}}
I need to create an XML-RPC server that gets cities with their corresponding IDs. What I do as a response is looking weird to me because of unnecessary duplicate entries but I couldnt find a better way.
Array
(
[cityID] => Array
(
[0] => 34
[1] => 35
[2] => 06
)
[cityName] => Array
(
[0] => Istanbul
[1] => Izmir
[2] => Ankara
)
)
I implemented above response. With this implementation:
$response = array(
array(
'cityID' => array(array('34', '35', '06'), 'array'),
'cityName' => array(array('Istanbul', 'Izmir', 'Ankara'), 'array')
),
'struct'
);
The problem is I want to take a response like this :
Array
(
[cities] => Array
(
['34'] => 'Istanbul'
['35'] => 'Izmir'
['06'] => 'Ankara'
)
)
So I tried to implement it like this :
$response = array(
array(
'cities' => array(array('34'=>'Istanbul', '35'=>'Izmir', '06'=>'Ankara'), 'array')
),
'struct'
);
But it fails with this implementation. What am I doing wrong ?
Thanks
You have array like following
$response = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
$newarray = array();
foreach($response['cityID'] as $key => $cityid){
$newarray['cities'][$cityid] = $response['cityName'][$key];
}
print_r($newarray);
You will be getting the expected array.
Array
(
[cities] => Array
(
[34] => Istanbul
[35] => Izmir
[6] => Ankara
)
)
This is how I do it, in Code Igniter 3
$array = array ( 'cityID' => array (
0 => 34,
1 => 35,
2 => 06
),
'cityName' => array(
0 => 'Istanbul',
1 => 'Izmir',
2 => 'Ankara'
)
);
foreach($array['cityID'] as $key => $cityid){
$response[] = array(array(
$cityid => array($array['cityName'][$key],'string'),
),'struct');
}
return $this->xmlrpc->send_response(array($response,'array'));
I have 2 multidimensional arrays that I am working with:
$arr1 =
Array
([type] => characters
[version] => 5.6.7.8
[data] => Array
([Char1] => Array
([id] => 1
[name] =>Char1
[title] =>Example
[tags] => Array
([0] => DPS
[1] => Support))
[Char2] => Array
([id] => 2
[name] =>Char2
[title] =>Example
[tags] => Array
([0] => Tank
[1] => N/A)
)
)
etc...
$arr2=
Array
([games] => Array
([gameId] => 123
[gameType => Match
[char_id] => 1
[stats] => Array
([damage] => 55555
[kills] => 5)
)
([gameId] => 157
[gameType => Match
[char_id] => 2
[stats] => Array
([damage] => 12642
[kills] => 9)
)
etc...
Basically, I need almost all the data in $arr2... but only the Char name from $arr1. How could I merge or add the $arr1['name'] key=>value into $arr2 where $arr1['id'] is equal to $arr2['char_id'] as the "id" field of each array is the same number.
I've attempted using array_merge and array_replace, but I haven't come up with any working solutions. This is also all data that I am receiving from a 3rd party, so I have no control on initial array setup.
Thanks for any help or suggestions!
Actually, this is quite straighforward. (I don't think there a built-in function that does this.)
Loop $arr2 and under it loop also $arr1. While under loop, just add a condition that if both ID's match, add that particular name to $arr2. (And use some referencing & on $arr2)
Consider this example:
// your data
$arr1 = array(
'type' => 'characters',
'version' => '5.6.7.8',
'data' => array(
'Char1' => array(
'id' => 1,
'name' => 'Char1',
'title' => 'Example',
'tags' => array('DPS', 'Support'),
),
'Char2' => array(
'id' => 2,
'name' => 'Char2',
'title' => 'Example',
'tags' => array('Tank', 'N/A'),
),
),
);
$arr2 = array(
'games' => array(
array(
'gameId' => 123,
'gameType' => 'Match',
'char_id' => 1,
'stats' => array('damage' => 55555, 'kills' => 5),
),
array(
'gameId' => 157,
'gameType' => 'Match',
'char_id' => 2,
'stats' => array('damage' => 12642, 'kills' => 9),
),
),
);
foreach($arr2['games'] as &$value) {
$arr2_char_id = $value['char_id'];
// loop and check against the $arr1
foreach($arr1['data'] as $element) {
if($arr2_char_id == $element['id']) {
$value['name'] = $element['name'];
}
}
}
echo '<pre>';
print_r($arr2);
$arr2 should look now like this:
Array
(
[games] => Array
(
[0] => Array
(
[gameId] => 123
[gameType] => Match
[char_id] => 1
[stats] => Array
(
[damage] => 55555
[kills] => 5
)
[name] => Char1 // <-- name
)
[1] => Array
(
[gameId] => 157
[gameType] => Match
[char_id] => 2
[stats] => Array
(
[damage] => 12642
[kills] => 9
)
[name] => Char2 // <-- name
)
)
)
Iterate over $arr2 and add the data to it from the matching $arr1 array value:
$i = 0;
foreach($arr2['games'] as $arr2Game){
$id = $arr2Game['char_id'];
$arr2['games'][$i]['name'] = $arr1['data'][$id]['name'];
$i++;
}
Have not tested this code.
If I'm understanding you correctly, you want to add a name index to each of the arrays within the $arr2['games'] array.
foreach($arr2['games'] as $key => $innerArray)
{
$arr2['games'][$key]['name'] = $arr1['data']['Char'.$innerArray['char_id']]['name'];
}