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])
}
}
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 an collection of bets, where i need to calculate var $profit.
Each record has stake and odds.
Here is my collection:
Illuminate\Database\Eloquent\Collection {#1900 ▼
#items: array:4 [▼
0 => App\Models\Bet {#1912 ▶}
1 => App\Models\Bet {#1906 ▶}
2 => App\Models\Bet {#1857 ▶}
3 => App\Models\Bet {#1882 ▶}
]
}
and attributes of array:
#attributes: array:19 [▼
"id" => 4
"user_id" => 1
"status_id" => 1
"content" => "some text"
"sport" => "Basketball"
"competition" => "Premier league"
"start_date" => "2021-12-08"
"bookmaker_id" => 1
"team1" => "team one"
"team2" => "team two"
"selection_id" => 1
"tip" => "2,5"
"odds" => "5"
"unit_id" => 5
"created_at" => "2021-12-06 13:32:46"
"updated_at" => "2021-12-06 13:32:46"
"created_by" => 1
"updated_by" => null
"deleted_by" => null
]
how can i make calculations in each collection array?
for exmpl to count profit (odds*unit_id(stake))
this was the thing i needed:
$bets = array_map(function($item) {
$item['won'] = number_format((double)$item['odds'] * (int)$item['unit_id'],3);
return $item;
}, $bets);
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);
I use smarty to display different code and I want to check if the array of some phrase contains more than 1 items.
I want to create a if phrase, that checks if the array contains only 1 value or more. Something like this, but of course that correct.
{if $domains|#array < 1}
How can I achieve that?
The code looks like this:
$domains
Smarty_Variable Object (3)
->value = Array (3)
0 => Array (17)
domain => "example1.com"
regperiod => "1"
dnsmanagement => "on"
emailforwarding => ""
idprotection => ""
addonsCount => 1
eppvalue => ""
fields => Array (0)
configtoshow => true
hosting => false
1 => Array (17)
domain => "example2.com"
regperiod => "1"
dnsmanagement => "on"
emailforwarding => ""
idprotection => ""
addonsCount => 1
eppvalue => ""
fields => Array (0)
configtoshow => true
hosting => false
2 => Array (17)
domain => "example3.com"
regperiod => "1"
dnsmanagement => "on"
emailforwarding => ""
idprotection => ""
addonsCount => 1
eppvalue => ""
fields => Array (0)
configtoshow => true
hosting => false
->nocache = false
You can use count (from the php function http://php.net/manual/es/function.count.php):
{if $domains|#count < 1}