How to find the lowest value of an array? - php

The result of print_r($data['SearchAvailResponse']['Hotel']) :
Array
(
[0] => Array
(
[HotelNo] => 1
[HCode] => IDJKT_00085
[Name] => Cebu Grand Hotel
[Currency] => USD
[TotalRate] => 56
)
[1] => Array
(
[HotelNo] => 2
[HCode] => IDJKT_00094
[Name] => Best Western Plus Lex Cebu
[Currency] => USD
[TotalRate] => 65
)
[2] => Array
(
[HotelNo] => 3
[HCode] => IDJKT_00102
[Name] => Best Western Sand Bar
[Currency] => USD
[TotalRate] => 93
)
[3] => Array
(
[HotelNo] => 4
[HCode] => IDJKT_00106
[Name] => Goldberry Suites & Hotel
[Currency] => USD
[TotalRate] => 51
)
)
TotalRate tag is hotel price
I want to find the lowest price of all hotels
How do I find the lowest price of all hotels?
Any help much appreciated
Cheers

$cheapesthotel = array();
foreach($data['SearchAvailResponse']['Hotel'] as $hotel){
if($cheapesthotel == array()){
$cheapesthotel = $hotel;
} elseif ($hotel['TotalRate'] < $cheapesthotel['TotalRate']){
$cheapesthotel = $hotel;
}
}

function getMin($array)
{
$currentMin = 10000;
$minObject = Array();
foreach($array as $val)
{
if($val[totalRate] < $currentMin)
{
$minObject = $val;
}
}
return minObject;
}

Related

Get Data from database using array values with for loop in PHP

Array:
$ecodesAr = Array (
[0] => 1Z0-060
[1] => 98-375
[2] => 98-368
[3] => 98-367 )
for($k=0; $k<count($ecodesAr); $k++){
$arrayTB[$k] = $this->Functions->exam('title', $ecodesAr[$k]); }
Modal code:
public function exam($q, $d) {
$q = $this->db->where($q, $d)
->get('exams');
return $q->row();
}
Result:
Array (
[0] =>
[1] =>
[2] =>
[3] => stdClass Object ( [id] => 1091 [hot_exam] => 0 [top_exam] => 0 [category] => 114 [subcats] => 288 [slug] => 98-367 [sku] => OI5Ao [title] => 98-367 [name] => MTA Security Fundamentals [update] => 2021-09-16 [regular_price] => 130 [sale_price] => 59 [on_homepage] => 0 [on_request] => 0 [expired] => 0 [path_slug] => 98-367.pdf [questions] => 123 [demo_slug] => 98-367-demo.pdf [prc_price] => 65 [prc_demo] => 98-367-demo [prc_exam] => 98-367 [is_active] => 1 )
)
The First 3 values are skiped in the output and just the last value got, I want to all array data against values please help anyone!
$productsarry = Array (
[0] => Milk
[1] => Cream
[2] => Sugar
[3] => Yougert
);
for($k = 0; $k < count($productsarry); $k++) {
$arrayTB[$k] = query("select slug, qty, name, price from exams where title ='$ecode'")->row();
}

partial search in multidimensional array

Hi i have an array like below. I want to search partial data from this array.
for example: i want to search "New Delhi" then i got array where city = Delhi, and search "Raigad" then got array where city = Raigarh
Array(
[56] => Array
(
[city] => Davangere
[product_id] => 14
[tier] => Tier 4
)
[57] => Array
(
[city] => Dehradun
[product_id] => 14
[tier] => Tier 3
)
[58] => Array
(
[city] => Delhi
[product_id] => 14
[tier] => Metro
)
[59] => Array
(
[city] => Delhi
[product_id] => 14
[tier] => Metro
)
[60] => Array
(
[city] => Raigarh
[product_id] => 14
[tier] => Metro
)
)
Make use of similar_text to attain this -
$finalArray = array();
$searchString = "New Delhi";
//Loop through your array
foreach ($your_array as $key => $value) {
similar_text($searchString, $value['city'], $percentageSimilarity);
//if percentage similarity between the text is above 70%, add to to our final array
if ($percentageSimilarity > 70) {
$finalArray[$key] = $value;
}
}
var_dump($finalArray);
Works for Delhi and Raigarh.

How insert array value in each index of another array in php?

I am having two arrays, in that i need to insert the each index of last key and value of another array keys, values in php. My sample arrays are given below. I am using codeigniter framework.
First array:
Array
(
[0] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Bacardi Rum
[quantity] => 1
[unit_price] => 25.00
[inv_discount] => 0.00
[salesman_id] => 25,27
)
[1] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Baileys
[quantity] => 1
[unit_price] => 15.00
[inv_discount] => 0.00
[salesman_id] => 28,29
)
)
Second array:
Array
(
[0] => 140140,150150
[1] => 151151,05180518
)
And i need the o/p :
Array
(
[0] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Bacardi Rum
[quantity] => 1
[unit_price] => 25.00
[inv_discount] => 0.00
[salesman_id] => 25,27
[salesman] => 140140,150150
)
[1] => stdClass Object
(
[customer_name] => Cash
[ordernumber] => 6452424
[product_name] => Baileys
[quantity] => 1
[unit_price] => 15.00
[inv_discount] => 0.00
[salesman_id] => 28,29
[salesman] => 151151,05180518
)
)
Can any one help me, give some ideas to solve this.
In this case, you have an array of objects (stdClass type) and one other array only. Answering your question you just have to do the code as shown below.
foreach ($secondArray as $key => $value) {
$firstArray[$key]->salesman = $value;
}
or
foreach ($firstArray as $key => $object) {
$object->salesman = $firstArray[$key];
}
foreach($arrA as $key=>$val){
$arrA[$key]['salesman'] = $arrB[$key];
}
As long as both arrays will be same size, array_map will be suitable:
$resultArray = array_map(function ($rowA, $rowB) {
$rowA->salesman = $rowB;
return $rowA;
}, $firstArray, $secondArray);

How to sort complex multidimensional array

i need to sort an array like this in function of the price:
Array
(
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
Can someone give me the right way to do that? :)
EXPECTED RESULT
Array
(
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
What i need is to order that array in function of price.
So if i have many solutions, i need to take the one that have minor price
You can use the PHP function usort like this:
function sortInnerPrice($a, $b) {
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
// First sort the inner prices
foreach ($test as $key => $val) {
usort($test[$key]['solutions'], 'sortInnerPrice');
}
function cmp($a, $b)
{
$aPrice = $a['solutions'][0]['price'];
$bPrice = $b['solutions'][0]['price'];
if ($aPrice == $bPrice) {
return 0;
}
return ($aPrice < $bPrice) ? -1 : 1;
}
// Then sort by lowest solution price
usort($yourArray, "cmp");
usort is a PHP function that allows you to sort an array based on whatever you want. You create a function that will return 0 if they are the same, -1 if you want $a before $b, and 1 if you want $a after $b.
Try to make another array containing all the prices and then use array_multisort for it

retrieve matching elements in array

I have an array like this
Array
(
[35635d5ebdd938d6360e65a9e2484073] => Array
(
[rowid] => 35635d5ebdd938d6360e65a9e2484073
[id] => MYU_SC3
[qty] => 1
[price] => 4800
[name] => JAMB UTME Scratch Card
[service_image] => assets/img/jamb.jpg
[service_category] => cards
[subtotal] => 4800
)
[d8df18561040f3d9bd9868f5c5aaa7c2] => Array
(
[rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
[id] => MYU_SC1
[qty] => 1
[price] => 1600
[name] => WAEC Scratch Card
[service_image] => assets/img/waec.jpg
[service_category] => cards
[subtotal] => 1600
)
[a4a751dd9a69824eb3abb6f49c7a7f61] => Array
(
[rowid] => a4a751dd9a69824eb3abb6f49c7a7f61
[id] => MYU_SC2
[qty] => 1
[price] => 1600
[name] => NECO Scratch Card
[service_image] => assets/img/neco.jpg
[service_category] => cards
[subtotal] => 1600
)
)
I want to retrieve the sub-arrays that match elements in another array
$card_skus = array("MYU_SC1","MYU_SC2","MYU_SC3");
Am looping through the main array
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
}
}
How do i get this done, thanks for the help
I guess #nickb already answered in the comments, didnt see it while writing an answer.
Try this
$subArray = array();
foreach ($this->cart->contents() as $key => $item) {
if(in_array($item['id'], $card_skus))
{
//Didn't know what to do at this point
$subArray[] = $item;
}
}
Use $subArray now as per your requirement

Categories