Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:08:00+00:00 2026-06-12T05:08:00+00:00

I Am trying to compare 3 different array prices in order to find the

  • 0

I Am trying to compare 3 different array prices in order to find the lowest so that I can decide which array values should be input into a database, the code looks something like this at the moment…

$array_a = array(
   "id"    => 398,
   "price" => 100 
);

$array_b = array(
   "id"    => 387,
   "price" => 60
);

$array_c = array(
   "id"    => 127,
   "price" => 50
);

if($array_a && $array_b && $array_c){
   $newArr = array($array_a['price'], $array_b['price'], $array_c['price']);
   array_keys($newArr, min($newArr));
   print_r($newArr)."\n"; 
}

The above code does not return the correct index of the array with the lowest price which in this case would be 2 (array_c), what would be the correct way to find out the key of the lowest value.

Also what would be the best way to make sure that only numbers are compared with the min() function as opposed to strings?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:08:01+00:00Added an answer on June 12, 2026 at 5:08 am

    You can automate it for example in this manner:

    $newArr = array($array_a['price'], $array_b['price'], $array_c['price']);
    asort($newArr, SORT_NUMERIC);
    echo "Minimum: ".reset($newArr).", given in array #".key($newArr);
    

    I ‘m not so sure how to answer your closing question — what should happen if the values are not actually typed as numbers?

    Update: Here’s one way to exclude non-numeric values:

    asort($newArr, SORT_NUMERIC);
    while (!is_numeric(current($newArr))) next($newArr);
    if (key($newArr) === null) {
        echo "No valid elements found";
    }
    else {
        echo "Minimum: ".current($newArr).", given in array #".key($newArr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compare two different arrays. I am looking for the values
I am trying to compare two arrays that always have different dimensions. eg. arr1
I'm trying to compare two different column types (not my doing, part of a
Trying to compare and print only members with Inactive status. The problem is that
I am trying to compare two tools execution time, which I have installed in
I am trying to compare values on data frame rows, and removing all the
I am trying to compare two variables in which are usually strings. These variables
I'm trying to compare 2 different char in assembly(calling assembly function in C program).
I'm trying to compare objects of a common base class together. The comparison should
I am trying to find an efficient way to sort an array of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.