2

I have a unique problem that I cannot seem to find an answer to.

I ultimately want to find the index of the array that has the min value on 'price'.

$r = array (
array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10));

You might also notice that the 'price' has duplicate values of 10. In this case, would it be possible to then compare the $r[0]['day'] and $r[3]['day'] values and then come up with $index=0? And if PRICE and DAY are the same, then either one is fine.

The result I'd like would be print "Free Shipping".$r[$index]['name']." ".$r[$index]['day']." Business Days".

EDIT I found out my array is different, there is no index but now a name reference.

$r = array ( 'DOM.EP' => array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10), 'DOM.PC' => array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20), 'DOM.IS' => array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100), 'DOM.SM' => array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10) );

LAST EDIT Thank you everyone for your help! I've gone ahead and combined code @Kolob Canyon and also @sathish R to get my final code.

$min = 9999999999;
$minDay = 9999999999;
$idx = null;
foreach ($r as $key => $value) {
    if($min > $r[$key]['price']) {
            $min = $r[$key]['price'];
            $minDay = $r[$key]['day'];
            $idx = $key;
        } 
        else if ($min == $r[$key]['price']) {
            if ($minDay > $r[$key]['day']){
                $minDay = $r[$key]['day'];
                $idx = $key;
            }
        }
}

if($idx != null) {
        echo "Free Shipping ". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
    }
4
  • Have a look at stackoverflow.com/questions/4497810/… Commented Jun 23, 2017 at 4:42
  • Let me know this is what you want:- eval.in/821197 Commented Jun 23, 2017 at 5:09
  • JJ_StackO did you checked the comments as well as answers? Commented Jun 23, 2017 at 6:54
  • Thanks everyone... ! It's been a while since I've programmed and all these are very helpful! I appreciate the communities responses. I'm going to go through them and see if I understand it all! Commented Jun 23, 2017 at 12:37

5 Answers 5

1

Feels like I'm solving your homework question:

$min = PHP_INT_MAX;
$idx = null;
for($i=0; $i < count($r); $i++)
{
       if($min > $r[$i]['price']) 
       { 
            $min = $r[$i]['price'];
            $idx = $i;
       }
}

if($idx != null) {
    echo "Free Shipping". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days"; 
}

Answer to your edit.

<?php
$r = array (
        'DOM.EP' => array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
        'DOM.PC' => array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
        'DOM.IS' => array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
        'DOM.SM' => array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10)
);

$min = PHP_INT_MAX;
$idx = null;
foreach ($r as $key => $value) {
        if($min > $r[$key]['price'])
        {
            $min = $r[$key]['price'];
            $idx = $key;
        }
    }

    if($idx != null) {
        echo "Free Shipping ". $r[$idx]['name'] . " " . $r[$idx]['day'] . " Business Days";
    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I havent programmed in a long time and I remember why I have this love/hate relationship with it :) I get enjoyment when I figure something out but get extremely frustrated when it doesn't return the results I want lol.
1

You could use simple for loop to achieve your result.

$r = array ( array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10), array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20), array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100), array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10)); $minPrice=PHP_INT_MAX ,$minDay =PHP_INT_MAX,$index=-1 ; $i=0; foreach($r as $d) { if($d['price'] < $minPrice) { $minPrice = $d['price']; $minDay = $d['day']; $index = $i; } else if($d['price'] == $minPrice ) { if($d['day'] < $minDay ) { $minDay = $d['day']; $index = $i; } } $i++; } if($index >=0 ) { echo "Free Shipping". $r[$index]['name'] . " " . $r[$index]['day'] . " Business Days"; }

2 Comments

I tried using this and it worked great! However, the my array was changed into a multidimensional without the index. $r = array ( 'DOM.EP' => array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10), 'DOM.PC' => array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20), 'DOM.IS' => array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100), 'DOM.SM' => array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10) );
I can reference the $r['DOM.EP']['name'] directly but confused how would $index pull 'DOM.EP' now? Would I use array_keys in replace of $index = $i?
0

you can do as following :

$dealersMin = min(array_column($dealers, 'count'));

$dealersWithMinCount = array_filter($dealers, function ($dealer) {
    global $dealersMin;
    return ($dealer['count'] == $dealersMin);
});

var_dump($dealersWithMinCount[array_rand($dealersWithMinCount)]['id']);

Hope this will work....

You can see more here https://eval.in/819623

2 Comments

using global is a bad practice. You can replace it with function ($dealer) use ($dealersMin)
Hi there! I did look at this one previously, it returned the value and not the specific key unfortunately.
0

Using native function asort to reorder your array

<?php

// your array
$r = array (
        array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
        array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
        array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
        array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10)
);

// getting the columns
$col_id     = array_column($r, 'id');   
$col_name   = array_column($r, 'name');
$col_day    = array_column($r, 'day');
$col_price  = array_column($r, 'price');


// reordering the columns - first the price, second the day
$new_r = array();

$i=0;
foreach($col_id as $id)
{
    $new_r[] = array(
                    'price'=>$col_price[$i],
                    'day'=>$col_day[$i],
                    'id'=>$id,
                    'name'=>$col_name[$i],
                    );
    $i++;
}

// asort the new array - php will do what you want following the order of the columns       
asort($new_r);      
var_dump($new_r);   

//Then get the desired element (the first in the array above)

$free = array_shift($new_r); //get first element of the array $new_r
print "Free Shipping: ".$free['name']." ".$free['day']." Business Days";

Output of the script:

array (size=4)
  0 => 
    array (size=4)
      'price' => int 10
      'day' => int 1
      'id' => int 1526
      'name' => string 'Expedited Parcel' (length=16)
  3 => 
    array (size=4)
      'price' => int 10
      'day' => int 15
      'id' => int 1332
      'name' => string 'Snail Mail Shipping' (length=19)
  1 => 
    array (size=4)
      'price' => int 20
      'day' => int 2
      'id' => int 1234
      'name' => string 'XpressPost Parcel' (length=17)
  2 => 
    array (size=4)
      'price' => int 100
      'day' => int 7
      'id' => int 5345
      'name' => string 'Internation Shipping' (length=20)

Free Shipping: Expedited Parcel 1 Business Days

Make tests changing the values of the prices and days.

Comments

0

Code :

<?php
$temp_array = array();
$r = array (
array('id' => 1526,'name'=>"Expedited Parcel",'day'=>1,'price'=>10),
array('id' => 1234,'name'=>"XpressPost Parcel",'day'=>2,'price'=>20),
array('id' => 5345,'name'=>"Internation Shipping",'day'=>7,'price'=>100),
array('id' => 1332,'name'=>"Snail Mail Shipping",'day'=>15,'price'=>10));

foreach($r as $row)
{
    if(count($temp_array)!=0)
    {
        if($temp_array['price']>$row['price'])
        {
            $temp_array['id'] = $row['id'];
            $temp_array['name'] = $row['name'];
            $temp_array['day'] = $row['day'];
            $temp_array['price'] = $row['price'];
        }
        else if($temp_array['price'] == $row['price'])
        {
            if($temp_array['day']>$row['day'])
            {
                $temp_array['id'] = $row['id'];
                $temp_array['name'] = $row['name'];
                $temp_array['day'] = $row['day'];
                $temp_array['price'] = $row['price'];
            }
        }
    }
    else
    {
        $temp_array['id'] = $row['id'];
        $temp_array['name'] = $row['name'];
        $temp_array['day'] = $row['day'];
        $temp_array['price'] = $row['price'];
    }
}
echo "Free Shipping :".$temp_array['name'].", ".$temp_array['day']." Business Days";
?>

Output :

Free Shipping :Expedited Parcel, 1 Business Days

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.