Hi i have problem with merge array,
How to merge array from different array
From Here
Array 1
array:6 [
"patient_name" => "Pasien 4"
"employee_no" => "1114"
"birth_date" => "1990-05-02"
"gender" => "L"
"department_code" => "D0004"
"section_code" => "S0004"
]
Array 2
array:2 [
"kd_layan" => "10000104 "
"nama_layan" => "PAKET MCU ADVANCE (MALE)"
]
To Here
array:8 [
"patient_name" => "Pasien 4"
"employee_no" => "1114"
"birth_date" => "1990-05-02"
"gender" => "L"
"department_code" => "D0004"
"section_code" => "S0004"
"kd_layan" => "10000104 "
"nama_layan" => "PAKET MCU ADVANCE (MALE)"
]
Any solution for this problem?
Thanks
It has very simple solution using array_merge() function of php.
array_merge() function merges one or more arrays into one array.
You can assign one array to the function, or as many as you like.
If two or more array elements have the same key, the last one overrides the others.
in your case use it as below
$arr1=[
"patient_name" => "Pasien 4",
"employee_no" => "1114",
"birth_date" => "1990-05-02",
"gender" => "L",
"department_code" => "D0004",
"section_code" => "S0004"
];
$arr2=[
"kd_layan" => "10000104 ",
"nama_layan" => "PAKET MCU ADVANCE (MALE)"
];
print_r(array_merge($arr1,$arr2));
for more see documentation
use array_merge to merge two array
$array1 = [
"patient_name" => "Pasien 4",
"employee_no" => "1114",
"birth_date" => "1990-05-02",
"gender" => "L",
"department_code" => "D0004",
"section_code" => "S0004",
];
$array2 = [
"kd_layan" => "10000104",
"nama_layan" => "PAKET MCU ADVANCE (MALE)"
];
$res = array_merge($array1, $array2);
echo '<pre>';
print_r($res);
check demo code
Use array_merge()
It merges one or more arrays into one array.
Syntax : array_merge(array1, array2, array3, ...)
use array_merge
$arr1 = [
"patient_name" => "Pasien 4"
"employee_no" => "1114"
"birth_date" => "1990-05-02"
"gender" => "L"
"department_code" => "D0004"
"section_code" => "S0004"
]
arr2 = [
"kd_layan" => "10000104 "
"nama_layan" => "PAKET MCU ADVANCE (MALE)"
]
$result = array_merge($arr1, $arr2);
echo '<pre>';
print_r($result);
Below i have mention an example which will merge two array and the output will be as it is as you want.
$a = array('1' => 'one','2' => 'two');
$b = array('3' => 'three','4' => 'four');
$c = ($a + $b);
print_r($c);
Related
So I have following array of hash:
my_array = [
{
"date" => "2022-12-01",
"pic" => "Jason",
"guard" => "Steven",
"front_desk" => "Emily"
},
{
"date" => "2022-12-02",
"pic" => "Gilbert",
"guard" => "Johnny",
"front_desk" => "Bella"
},
{
"date" => "2022-12-03",
"pic" => "Steven",
"guard" => "Gilbert",
"front_desk" => "Esmeralda"
}
]
My question is how do I change the structure of my array (grouping) by date in Ruby (Rails 7). Or in other word, I want to change my array into something like this:
my_array = [
{
"2022-12-01" => {
"pic" => "Jason",
"guard" => "Steven",
"front_desk" => "Emily"
{
},
{
"2022-12-02" => {
"pic" => "Gilbert",
"guard" => "Johnny",
"front_desk" => "Bella"
}
},
{
"2022-12-03" => {
"pic" => "Steven",
"guard" => "Gilbert",
"front_desk" => "Esmeralda"
}
}
]
Anyway, thanks in advance for the answer
I have tried using group_by method to group by its date, but it doesn't give the output I wanted
I've tried this method:
my_array.group_by { |element| element["date"] }.values
If you simply want a 1:1 mapping of your input objects to an output object of a new shape, then you just need to use Array#map:
my_array.map {|entry| {entry["date"] => entry.except("date")} }
(Hash#except comes from ActiveSupport, and is not standard Ruby, but since you're in Rails it should work just fine).
Both solutions assume the key "date" will be unique. If we cannot make this assumption safely, then each date should be mapped to an array of hashes.
my_array.each_with_object({}) do |x, hsh|
date = x["date"]
hsh[date] ||= []
hsh[date] << x.except("date")
end
Result:
{
"2022-12-01" => [
{"pic"=>"Jason", "guard"=>"Steven", "front_desk"=>"Emily"}
],
"2022-12-02" => [
{"pic"=>"Gilbert", "guard"=>"Johnny", "front_desk"=>"Bella"}
],
"2022-12-03" => [
{"pic"=>"Steven", "guard"=>"Gilbert", "front_desk"=>"Esmeralda"}
]
}
Or you may like:
my_array
.sort_by { |x| x["date"] }
.group_by { |x| x["date"] }
.transform_values { |x| x.except("date") }
Good day everyone, I have this function that can generate time interval and store them to $time.
if(strtotime($startTime) <= strtotime($endTime))
{
$this->time[$i]['room'] = '49';
$this->time[$i]['day'] = 'T-Th';
$this->time[$i]['c_time'] = $start.'-'.$end;
$this->time[$i]['sy'] = '2021-2022';
$this->time[$i]['sem'] = '1st';
}
Sample output of $time is like this
1 => array:5 [▼
"room" => "49"
"day" => "T-Th"
"c_time" => "07:00-08:30"
"sy" => "2021-2022"
"sem" => "1st"
]
2 => array:5 [▼
"room" => "49"
"day" => "T-Th"
"c_time" => "08:30-10:00"
"sy" => "2021-2022"
"sem" => "1st"
]
3 => array:5 [▼
"room" => "49"
"day" => "T-Th"
"c_time" => "10:00-11:30"
"sy" => "2021-2022"
"sem" => "1st"
]]
What should I do so that the output would be a collection->toArray() just like this
array:5 [▼
0 => {#1416 ▼
+"room": "49"
+"day": "M-W"
+"c_time": "13:00-14:00"
+"sy": "2021-2022"
+"sem": "1st"
}
1 => {#1435 ▼
+"room": "49"
+"day": "M-W"
+"c_time": "11:30-13:00"
+"sy": "2021-2022"
+"sem": "1st"
}
2 => {#1433 ▼
+"room": "49"
+"day": "M-W"
+"c_time": "13:00-14:30"
+"sy": "2021-2022"
+"sem": "1st"
}]
Since you did not provide us with where you define the $time variable, I can't help you with any issues there. However, first, instantiate that variable as a collection.
$this->time = collect([]);
And then you can push in the following manner.
$this->time->put($i, (object) [
'room' => '49',
'day' => 'T-Th',
'c_time' => $start.'-'.$end,
'sy' => '2021-2022',
'sem' => '1st',
]);
The simplest way to convert an array to collection is to use Laravel's collect() helper function. It takes an array as a parameter and returns a collection type.
For example if we have this array:
$a = [
['name' => "abc", 'age' =>45],
['name' => "xyz", 'age' =>20],
];
dd(collect($a));
The output will be:
I have a bunch of Hashes inside of an array. When checking my keys and values I get the expected output except for some special cases as they refer to more Arrays/Hashes.
Think of something like this:
#AoH = ( { 'husband' => "homer", 'wife' => "marge" },
{ 'people' => [{'Bob'=> 24, 'Lukas'=> 37}] },
{ 'vegetables' => { 'tomato' => "red", 'carrot' => "orange"} });
My function iterates through the array and displays my keys and values as in the following:
sub function(...){
print "$key => $value\n";
}
husband => homer
wife => marge
people => ARRAY(0x6b0d80)
Bob => 24
Lukas => 37
vegetables => HASH(0x2570d38)
tomato => red
carrot => orange
Now I want to access my keys and values, but when getting something like ARRAY or HASH as value, I want to disregard that hash and not print it.
Is there some kind of way to only access Values with type scalar?
So far I tried this:
if ($value eq 'ARRAY') {
}
elsif ($value eq ref {}) {
}
else {
print "$key => $value\n";
}
But, it ends up printing exactly the same as above and does not disregard the other data structures.
For an arbitrary data structure like yours, you can use Data::Traverse:
use warnings;
use strict;
use Data::Traverse qw(traverse);
my #AoH = ( { 'husband' => "homer", 'wife' => "marge" },
{ 'people' => [{'Bob'=> 24, 'Lukas'=> 37}] },
{ 'vegetables' => { 'tomato' => "red", 'carrot' => "orange"} });
traverse { print "$a => $b\n" if /HASH/ } \#AoH;
Output:
wife => marge
husband => homer
Bob => 24
Lukas => 37
carrot => orange
tomato => red
Following demo code does not utilize external modules, provided for educational purpose.
use strict;
use warnings;
use feature 'say';
my #AoH = ( { 'husband' => "homer", 'wife' => "marge" },
{ 'people' => [{'Bob'=> 24, 'Lukas'=> 37}] },
{ 'vegetables' => { 'tomato' => "red", 'carrot' => "orange"} });
drill_in( \#AoH );
sub drill_in {
my $data = shift;
if( ref $data eq 'ARRAY' ) {
drill_in($_) for #$data;
} elsif ( ref $data eq 'HASH' ) {
while( my($k, $v ) = each %{$data} ) {
(ref $v eq 'ARRAY' or ref $v eq 'HASH') ? drill_in($v) : say "$k => $v";
}
}
}
Output
husband => homer
wife => marge
Lukas => 37
Bob => 24
tomato => red
carrot => orange
i have an array like
array:5 [▼
188 => array:17 [▼
"user_id" => "176"
"product_id" => "188"
"qty" => "2"
"date" => "03-05-2020"
"product_type" => "rear type"
"custom_color_title" => ""
"custom_color_price" => ""
"bolt_title" => ""
"bolt_price" => ""
"hub_center_rings_title" => ""
"hub_center_rings_price" => ""
"wheel_spacers_title" => ""
"wheel_spacers_price" => ""
"tire_pressure_title" => ""
"tire_pressure_price" => ""
"product_price" => 1890
"product_size" => ""
]
176 => array:17 [▼
"user_id" => ""
"product_id" => "176"
"qty" => "2"
"date" => "03-05-2020"
"product_type" => "wheel type"
"custom_color_title" => ""
"custom_color_price" => ""
"bolt_title" => ""
"bolt_price" => ""
"hub_center_rings_title" => ""
"hub_center_rings_price" => ""
"wheel_spacers_title" => ""
"wheel_spacers_price" => ""
"tire_pressure_title" => ""
"tire_pressure_price" => ""
"product_price" => 1680
"product_size" => ""
]
224 => array:17 [▶]
]
from a session variable this array
and mysql table fields are id,name,img etc.. how to join the array.product_id and table.id ,
my query like $table=DB::select('SELECT * FROM products');i am doing in laravel any way to join mysql table and array?
Your mysql dump which you want to join is missing. But i think i understand you want:
foreach($sessionArray as $sessionArrayKey => $sessionArrayVal)
{
$findInDb = array_search($sessionArrayVal['product_id'], array_column($dbArray, 'id'));
if ($findInDb)
{
$sessionArray[$sessionArrayKey] = array_merge($sessionArray[$sessionArrayKey],$dbArray[$findInDb])
}
}
This question already has answers here:
Ruby: How to concatenate array of arrays into one
(3 answers)
Closed 5 years ago.
I have an array containing of subarrays like this:
arr = [[{"big" => "2055", "small" => -"-10", "thin" => "i"},
{"big" => "2785", "small" => "0", "thin" => "l"}],
[{"big" => "7890", "small" => "3", "thin" => "t"},
{"big" => "2669", "small" => "0,5", "thin" => "f"},
{"big" => "9000", "small" => "2", "fat" => "O"}]]
I want to add subarrays to itself to get an array like this:
arr = [{"big" => "2055", "small" => "-10", "thin" => "i"},
{"big" => "2785", "small" => "0", "thin" => "l"},
{"big" => "7890", "small" => "3", "thin" => "t"},
{"big" => "2669", "small" => "0,5", "thin" => "f"},
{"big" => "9000", "small" => "2", "fat" => "O"}]
Doing like this:
arr.map! {|x| x+x}
I get added subarrays but every hash appears twice. How to do it right?
Do you just want to flatten the array?
arr.flatten
It concatenates every sub-array into one big array, recursively if needed.
[[1,2], [3,4]].flatten
# => [1, 2, 3, 4]
If you want to modify the array in place, you can use :
arr.flatten!
It doesn't matter what the inner-elements look like (integers, strings, hashes), as long as they're not arrays, they won't be touched by flatten.
Ruby have a built in method try use Array.flatten
You can use flatten!:
arr.flatten!
#=> [{"big"=>"2055", "small"=>"-10", "thin"=>"i"},
{"big"=>"2785", "small"=>"0", "thin"=>"l"},
{"big"=>"7890", "small"=>"3", "thin"=>"t"},
{"big"=>"2669", "small"=>"0,5", "thin"=>"f"},
{"big"=>"9000", "small"=>"2", "fat"=>"O"}]
You can try this
arr = arr.flatten