3

Updated Code that creates array, but ISNT outputting the min/max values. Why isnt this working and how do I get it to automatically remove the min/max value and then reoutput the array?

<?php 
$url = 'https://www.googleapis.com/shopping/search/v1/public/products?key=thekey&country=US&q=nintendo+wii';

$data = curl_init($url);
curl_setopt($data, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($data, CURLOPT_HEADER, 0);

$product_result = curl_exec($data);
curl_close($data);


$arr = json_decode($product_result, true);
$prices = array();

echo "Original Values";

foreach ($arr['items'] as $item)
{
    if (isset($item['product']['inventories'][0]['price']))
    {
        $prices[] = $item['product']['inventories'][0]['price'];
    }
}


echo '<pre>';
print_r($prices);
echo '<br /><br />';

// Get the values
$min = $prices[$minIndex];
$max = $prices[$maxIndex];

// Find the values
$minIndex = array_search($min, $prices);
$maxIndex = array_search($max, $prices);

#print out results with no min/max pair
echo $prices[$min] . 'max is ' . $prices[$max];
echo '</pre>';
// Unset the values
unset($prices[$minIndex], $prices[$maxIndex]);
?>

Current Output of the code that doesn't seem to show the min/max value:

Original Values
Array
(
    [0] => 149.99
    [1] => 149.99
    [2] => 149.99
    [3] => 209.95
    [4] => 124.99
    [5] => 225.99
    [6] => 149.96
    [7] => 249.99
    [8] => 193.99
    [9] => 149.99
    [10] => 149.99
    [11] => 149.99
    [12] => 326.99
    [13] => 269.96
    [14] => 258.99
    [15] => 129.99
    [16] => 149.99
    [17] => 39.99
    [18] => 149.99
    [19] => 209.99
    [20] => 349.95
    [21] => 357.38
    [22] => 169.96
    [23] => 125
    [24] => 149.99
)


max is 
2
  • 1
    one way: unset($prices[array_search($minval)]);unset($prices[array_search($maxval)]) Commented Dec 12, 2011 at 1:10
  • I saw this unset way but only saw key examples on php.net. Anyway to then reset the array in order to run the next line being: echo "min is ". $minval . "<br>Max is ". $maxval . "<br />avg is " . $avgofval ; it still appears to be calculating on the initial array with orig max/min pair. Commented Dec 12, 2011 at 1:17

3 Answers 3

1

Based on unset and array_search from php.net, this should work:

// Find the values
$minIndex = array_search($minVal, $prices);
$maxIndex = array_search($maxVal, $prices);

// Get the values
$min = $prices[$minIndex];
$max = $prices[$maxIndex];

// Unset the values
unset($prices[$minIndex], $prices[$maxIndex]);

// Print out values
echo 'min value: '.$min;
echo 'max value: '.$max;

// Print full array
print_r($prices);
Sign up to request clarification or add additional context in comments.

6 Comments

@Dagon Sorry, I published my answer before I saw your comment.
this provides a warning that: array_search accepts 2 or more parameters?
Please see updated code and errors I'm getting. I appreciate it Jon/Dagon!
This gets rid of the warning, but now it is not finding and getting rid of the min/max of the originally fetched array values. How do I get it to display these original min/max values, but then output the array WITHOUT the min/max original values?
Are you trying to echo $prices[$min]? Because all you have to do is echo $min, since it has the value already.
|
0

You may sort the array in numeric order and then use array_pop() and array_shift()

  sort($prices,SORT_NUMERIC);
  $maxval    = array_pop($prices);
  $minval    = array_shift($prices);

  $avgofval  = (count($prices))
                 ? array_sum($prices)/count($prices)
                 : 0;

  echo "min is ". $minval . "<br>Max is ". $maxval . "<br />avg is " . $avgofval ;
  echo '<pre>'.print_r($prices,1).'</pre>';

3 Comments

That requires 3 statements, your approach requires 5(2 for finding the values using min/max, 2 for finding the keys and 1 for deleting the values)
My approach is split up for reading purposes, I could do the entire thing as a one-liner, but there really isn't a point to showing that.
No matter how many lines you will use, you will still need 5 function-calls,
0

$minVal and $maxVal are not defined before the array search call.

// Find the values
$minIndex = array_search($minVal, $prices);
$maxIndex = array_search($maxVal, $prices);

And array_search doesn't find any value. It find the keys: Returns the key for needle if it is found in the array, FALSE otherwise.

$minIndex is FALSE, and $maxIndex is also FALSE.

The values are also incorrect...

// Get the values
$min = $prices[$minIndex]; // $min = $prices[FALSE]
$max = $prices[$maxIndex]; // $max = $prices[FALSE]

A possible solution is:

$minValue = FALSE;
$maxValue = 0;
foreach ($arr['items'] as $item) {
    if (isset($item['product']['inventories'][0]['price']) !== false) {
        $price = $item['product']['inventories'][0]['price'];
        $prices[] = $price;
        if ( ! $minValue || $minValue < $price) {
            $minValue = $price;
        }
        if ($price > $maxValue) {
            $maxValue = $price;
        }
    }
}

Aside

if (isset($item['product']['inventories'][0]['price']) !== false) { ... }

is a redundant test. It's exactly the same as

if (isset($item['product']['inventories'][0]['price'])) { ... }

1 Comment

This doesnt do much. Please see updated code. I appreciate your reply

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.