I have an array output in laravel, the structure looks like follows:
array:1 [
0 => {#35
+"id": 152
+"user_id": 123
+"post_id": 456
+"content": "impedit"
+"created_at": "04-09-2016 01:24"
+"updated_at": "2016-09-04 01:24:51"
}
]
How to test the array structure contains keys id、user_id ...?
I tried assertArrayHasKey('user_id', $array),but it shows failed.
Thanks.
Thanks for the #linuxartisan. Finally I convert the value object to array for testing.
$this->assertArrayHasKey('user_id', (array)$comment[0]);
Related
I am trying to get the daykey from dates which i need to use in Array to fill "0" if data is not available .
My dates start from "2022-02-08" and end with "2022-03-10".
Trying to get daykey using code
$rahul = Buffalomilkrecord::select(DB::raw('date'), DB::raw('sum(totalmilk) as totalmilk, DATE_FORMAT(date,"%d") as "daykey"'))
->whereBetween('date', [$olddate, $todaydate] )
->groupBy(DB::raw('date'))
->orderBy('date','asc')
->get();
the out put i am getting
array:31 [▼
0 => "2022-03-01"
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => 0
7 => "2022-02-08"
8 => 0
9 => 0
10 => 0
11 => 0
12 => 0
13 => 0
14 => 0
15 => "2022-02-16"
16 => "2022-02-17"
17 => "2022-02-18"
18 => "2022-02-19"
19 => "2022-02-20"
20 => "2022-02-21"
21 => "2022-02-22"
22 => "2022-02-23"
23 => 0
24 => "2022-02-25"
25 => "2022-02-26"
26 => 0
27 => 0
28 => 0
29 => 0
30 => 0
]
$ddtest = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
foreach($rahul as $order)
{
$ddtest[$order->daykey-1] = $order->date;
}
Here i am expecting to start array with
0 => "2022-02-08" and so on.... as date start in Month and end with March
I feel i am doing some error in query but can not find where i am doing mistake..
Thanks in advance for help
The SQL will only return exiting data, so if there is no data for this specific day, you wont get a row for this day. This is something you have to take care outside of the query.
You need to build the date range by yourself. for example like this:
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
Build one array with "default" entries per day with 0 values and than merge them with the sql result.
I'm using Elastic stack. There are a lot of messages which are parsed by my Logstash. I've decided to add some additional rules for Logstash.
I've installed Syslog pri plugin to my Logstash, because I want to create some mapping for my syslog's severity levels.
All of my messages has syslog_pri values according to RFC-3164, where error messages has "(3, 11, 19, ..., 187)" values of syslog_pri.
Well, I have two problems:
1) It's not very usable for me, because querying via Kibana is not usable. When I want to filter errors, it looks like:
syslog_pri: (3 OR 11 OR 19 OR 27 OR 35 OR 43 OR 51 OR 59 OR 67 OR 75 OR 83 OR 91 OR 99 OR 107 OR 115 OR 123 OR 131 OR 139 OR 147 OR 155 OR 163 OR 171 OR 179 OR 187)
but it will be much easier with syslog_pri plugin. I expect to have something like this:
syslog_pri: "error"
Is it possible to create this mapping somehow?
2) I want to change this syslog_pri value for some specific messages.
For example, I'm catching message like "Hello world" and want to change the severity from 14 (info messages) into 11 (error message).
I'm doing something like this:
filter {
grok {
match => { "message" => "..." }
}
syslog_pri { }
if "Hello world" in [message]
{
mutate { syslog_pri => 11 }
}
But this failed with an error:
logstash.filters.mutate - Unknown setting 'syslog_pri' for mutate
Suggestions?
To use the syslog_pri filter, you simply need to have a field with the value, which in turn will be decoded by the filter. If you have a field which is already named syslog_pri, then using it is as simple as putting
syslog_pri { }
in your logstash configuration.
This plugin will create 4 additional fields which will contain the decoded syslog_pri information:
syslog_facility
syslog_severity
syslog_facility_code
syslog_severity_code
As for mutating a field the syntax is as follows.
mutate {
replace => { "syslog_pri" => "11"}
}
I have the following two collections:
Collection {#402 ▼
#items: array:1 [▼
4007 => "4007 - Container Deposit - 18.00 Drum - In Stock: 0.00"
]
}
Collection {#398 ▼
#items: array:3 [▼
1000 => "1000 - Acetone - 162.00 KG - In Stock: 10000.00"
1001 => "1001 - Acetone - 15.80 KG - In Stock: 0.00"
24662 => "24662 - 1L Untd Antifreeze Orange FO2272A60(Prem - 1.00 Litre - In Stock: 0.00"
]
}
Using Laravel's collection merge function:
$merged = $ref_prod_containers->merge($ref_cust_prod);
dd($merged);
I get the following:
Collection {#397 ▼
#items: array:4 [▼
0 => "4007 - Container Deposit - 18.00 Drum - In Stock: 0.00"
1 => "1000 - Acetone - 162.00 KG - In Stock: 10000.00"
2 => "1001 - Acetone - 15.80 KG - In Stock: 0.00"
3 => "24662 - 1L Untd Antifreeze Orange FO2272A60(Prem - 1.00 Litre - In Stock: 0.00"
]
}
However I wish to retain the original keys. The merge function is removing them and replacing with 0,1,2,3.
Thanks, Julian
You can use Laravel Collection's union() method. Beware that this behaves differently from merge() when dealing with duplicate keys: if the same key is present in both $array1 and $array2 and you go $merged = $array1->union($array2), then the value of $array1 will end up in the $merged collection, and the value of $array2 will be discarded (Laravel union documentation).
I would try to use string keys for the merging and merged collection.
From the laravel docs section collections, function merge()
If the given array's keys are numeric, the values will be appended to the end of the collection:
Sorry for silly question, from below can anyone advise how can I get the output above? I tried echo $xml->0, but as usual, it is syntax error...
Output:
SimpleXMLElement Object
(
[0] => 03 04 32N 101 46 43E
)
SimpleXML returns arrays of SimpleXMLElements, thus use $xml[0].
I've got a text file where I have a lot of lines that look like the first example and only a couple that look like the second (NB the ** are just to show the fields I'm after, they don't look like this in the actual file);
22034 BUBBA C BC-022 **OWL SOFTIE** <N/A> <N/A> <N/A> 470 0.00 **6** 0.00 **1** **37.95**
22489 BUBBA C BC- **BUNNY BOO BOO** <N/A> <N/A> <N/A> 470 0.00 **2** 0.00 **1** **24.95**
My aim is to extract the ** surrounded fields into a format (probably csv) so I can add it to as a sheet to an existing Excel spreadsheet.
My issue is I can't figure out how to extract just the data I need using gsub, split, tr, scan, match etc.
My initial thinking was as I parsed each line, I'd delete up to the 4th instance of a space (which I can't find code for), then delete/skip everything between the first < and the last >, then try and delete the next 2 fields, keep 1, delete the next and keep the remaining 2.
All of which seems a bloody hard way to get to the end result.
I don't want the exact code to solve this problem, I'm more after the methodology you would go through when you're looking at this type of problem and what tools you'd use. (strip, gsub etc)
Any help greatly appreciated.
Use #split/#join pair to proceed:
a='22034 BUBBA C BC-022 **OWL SOFTIE** <N/A> <N/A> <N/A> 470 0.00 **6** 0.00 **1** **37.95**'.split
[ a[4..-10].join( ' ' ), a[-4], a[-2], a[-1] ].join ' '
# => "**OWL SOFTIE** **6** **1** **37.95**"
Space-delimited file, huh? That's not the most... optimal... format.
Anyway, I would use regex to snag that **OWL SOFTIE** field
[7] pry(main)> m = s.match /BC-\d*\s(.*?)\s</
=> #<MatchData "BC-022 OWL SOFTIE <" 1:"OWL SOFTIE">
[8] pry(main)> m.captures[0]
=> "OWL SOFTIE"
and then split to grab everything else.
[11] pry(main)> arr = s.split[-4..-1]
=> ["6", "0.00", "1", "37.95"]
[12] pry(main)> arr.select.with_index {|x,i| i!=1 }
=> ["6", "1", "37.95"]
Altogether:
[13] pry(main)> [s.match(/BC-\d*\s(.*?)\s</).captures[0]] + s.split[-4..-1].select.with_index {|x,i| i!=1 }
=> ["OWL SOFTIE", "6", "1", "37.95"]
(if you have any control over that input file whatsoever, see if you can make it delimited by something other than spaces :))
You only have one troublesome field with a variable number of words in it so start with split:
a = "22034 BUBBA C BC-022 OWL SOFTIE <N/A> <N/A> <N/A> 470 0.00 6 0.00 1 37.95".split
Then pick it apart:
[a[4..-10].join(' '), a[-4], a[-2], a[-1]]