Smarty check if items in array more than 1 - arrays

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}

Related

Laravel How to Make Array into a Collection?

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:

Check data structure and disregard if hash or array

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

How to join a array and MySQL table in Laravel

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

Solr Solaruim Client->ping is failing

I am new to this stuff. I just inherited this olde system! I managed to get Solr running, and am trying to execute some old code that rebuilds the Solr data. The client ping is failing and I don't understand why.
Here is the code that is trying to initiate a re-index
// these are correct, now Solarium is falling over
// with a 404. pile of junk
print(SOLR_SERVER_HOSTNAME . "\n");
print(SOLR_SERVER_PORT . "\n");
print(SOLR_SERVER_CORE . "\n");
$options = array(
'adapteroptions' => array(
'host' => SOLR_SERVER_HOSTNAME,
'port' => SOLR_SERVER_PORT,
'core' => SOLR_SERVER_CORE,
'timeout' => 60
)
);
}
try {
// Ping the Solr server to check its availability
// create a new client. This seems to work
$this->client = new Solarium_Client($options);
if ($this->client) {
print("Solarium returned something \n");
print_r($this->client);
print("\n");
} else {
echo 'Solarium screwed the pooch';
}
// and create a ping whatever that means
// this also seems to work
$ping = $this->client->createPing();
if ($ping) {
print("ping creation worked \n");
print_r($ping);
} else {
print("ping creation failed \n");
}
// this always fails and terminate the
// program run
$this->client->ping($ping);
} catch (Solarium_Exception $e) {
print("exeption from ping request \n");
throw $e;
}
print("about to reset \n");
// Reset all filters and queries and process request params if requested
$this->reset($request);
}
The Solr admin screen is up. I have an empty collection1..
Here is the output from the trace I stuck in here
1/tmp/reindex.lock
15/01/18 17:38:54 [INFO] Index started
Index started
localhost
9999
collection1
Solarium returned something
Solarium_Client Object
(
[_options:protected] => Array
(
[adapter] => Solarium_Client_Adapter_Http
[adapteroptions] => Array
(
[host] => localhost
[port] => 9999
[core] => collection1
[path] => /solr/
[timeout] => 60
)
)
[_queryTypes:protected] => Array
(
[select] => Array
(
[query] => Solarium_Query_Select
[requestbuilder] => Solarium_Client_RequestBuilder_Select
[responseparser] => Solarium_Client_ResponseParser_Select
)
[update] => Array
(
[query] => Solarium_Query_Update
[requestbuilder] => Solarium_Client_RequestBuilder_Update
[responseparser] => Solarium_Client_ResponseParser_Update
)
[ping] => Array
(
[query] => Solarium_Query_Ping
[requestbuilder] => Solarium_Client_RequestBuilder_Ping
[responseparser] => Solarium_Client_ResponseParser_Ping
)
[mlt] => Array
(
[query] => Solarium_Query_MoreLikeThis
[requestbuilder] => Solarium_Client_RequestBuilder_MoreLikeThis
[responseparser] => Solarium_Client_ResponseParser_MoreLikeThis
)
[analysis-document] => Array
(
[query] => Solarium_Query_Analysis_Document
[requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Document
[responseparser] => Solarium_Client_ResponseParser_Analysis_Document
)
[analysis-field] => Array
(
[query] => Solarium_Query_Analysis_Field
[requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Field
[responseparser] => Solarium_Client_ResponseParser_Analysis_Field
)
[terms] => Array
(
[query] => Solarium_Query_Terms
[requestbuilder] => Solarium_Client_RequestBuilder_Terms
[responseparser] => Solarium_Client_ResponseParser_Terms
)
[suggester] => Array
(
[query] => Solarium_Query_Suggester
[requestbuilder] => Solarium_Client_RequestBuilder_Suggester
[responseparser] => Solarium_Client_ResponseParser_Suggester
)
)
[_pluginTypes:protected] => Array
(
[loadbalancer] => Solarium_Plugin_Loadbalancer
[postbigrequest] => Solarium_Plugin_PostBigRequest
[customizerequest] => Solarium_Plugin_CustomizeRequest
[parallelexecution] => Solarium_Plugin_ParallelExecution
[bufferedadd] => Solarium_Plugin_BufferedAdd
[prefetchiterator] => Solarium_Plugin_PrefetchIterator
)
[_pluginInstances:protected] => Array
(
)
[_adapter:protected] =>
[_requestBuilders:protected] =>
)
ping creation worked
Solarium_Query_Ping Object
(
[_options:protected] => Array
(
[resultclass] => Solarium_Result_Ping
[handler] => admin/ping
)
[_helper:protected] =>
[_params:protected] => Array
(
)
)
exeption from ping request
Failed to initialise Solr client:
Solr HTTP error: Not Found (404)
Failed to initialise Solr client: Solr HTTP error: Not Found (404)
Any hints? It's the first time I have seen this stuff.
Cheers,
Mark.
Fixed. The core name was getting flattened in another bit of code. Two constants for the same property. Two different values! – Addinall 22 secs

Sorting an array of hashes which have alphanumeric values

I am facing an issue with sorting an array of hashes; hashes having alphanumeric values by which I need to sort.
Refer to this question! My question is an extension of this problem.
I tried the solution in the above question but didn't get the required output.
$arr_ref = [
{ brand => "A.1", PO => "1.a", supplier => "X" },
{ brand => "A.2", PO => "2.a", supplier => "X" },
{ brand => "B.1", PO => "1.b", supplier => "X" },
{ brand => "B.2", PO => "2.b", supplier => "X" },
{ brand => "B.3", PO => "1.c", supplier => "Y" },
]
I need to sort by Brand or PO.
#sort the array reference and place the array back into the standard_set
$arr_ref = [sort by_brand #$arr_ref];
sub by_brand {
$a->{brand} cmp $b->{brand}
}
Complexity is the key; can start with numeric or alphabetic character.
The Brand or PO can be of different size, as well. The delimiter may be a dot or hypen.
Can we solve this depending on the input received in $arr_ref?
You were really close. You just need to remove the [ and ] brackets and dereference the array you want to assign to #$array_ref = ....
use strict;
use warnings;
my $arr_ref = [
{ brand => "B.3", PO => "1.c", supplier => "Y" },
{ brand => "B.2", PO => "2.b", supplier => "X" },
{ brand => "B.1", PO => "1.b", supplier => "X" },
{ brand => "A.2", PO => "2.a", supplier => "X" },
{ brand => "A.1", PO => "1.a", supplier => "X" },
];
my #sorted = sort { $a->{brand} cmp $b->{brand} } #$arr_ref;
use Data::Dump;
dd #sorted;
Output:
(
{ brand => "A.1", PO => "1.a", supplier => "X" },
{ brand => "A.2", PO => "2.a", supplier => "X" },
{ brand => "B.1", PO => "1.b", supplier => "X" },
{ brand => "B.2", PO => "2.b", supplier => "X" },
{ brand => "B.3", PO => "1.c", supplier => "Y" },
)
Try this sort method:
sub by_brand_or_po {
my ($a_ba, $a_bn) = split /\.|-/, $a->{brand};
my ($b_ba, $b_bn) = split /\.|-/, $b->{brand};
my ($a_pa, $a_pn) = split /\.|-/, $a->{PO};
my ($b_pa, $b_pn) = split /\.|-/, $b->{PO};
return( $a_ba cmp $b_ba or $a_bn <=> $b_bn or
$a_pa cmp $b_pa or $a_pn <=> $b_pn );
}
It prioritizes the brand alpha over numeric over PO alpha over PO numeric, and will split over a dot or hyphen.
Sort it using Schwartzian transform and correct comparator for each field.
use strict;
use warnings;
use Data::Dumper;
my $arr_ref = [
{ brand => "A.1", PO => "1.a", supplier => "X" },
{ brand => "A.2", PO => "2.a", supplier => "X" },
{ brand => "B.1", PO => "1.b", supplier => "X" },
{ brand => "B.2", PO => "2.b", supplier => "X" },
{ brand => "B.3", PO => "1.c", supplier => "Y" },
];
my #sorted = map $_->[0], sort {
$a->[1][0] cmp $b->[1][0]
or $a->[1][1] <=> $b->[1][1]
or $a->[1][2] <=> $b->[1][2]
or $a->[1][3] cmp $b->[1][3]
} map [ $_, [ map split( /[.-]/, $_, 2 ), #$_{qw(brand PO)} ] ],
#$arr_ref;
print Dumper( \#sorted );
Try use brand "A.10" to spot the difference.
use Sort::Key::Natural!
use Sort::Key::Natural qw(natkeysort);
my #sorted_by_brand = natkeysort { $_->{brand} } #data;
my #sorted_by_po = natkeysort { $_->{po} } #data;

Resources