0

I have a website that allows people to buy items for specific games. And i have a page that shows all available stock on 200+ items. The information comes from the Steam API.

I use this code to count the items available.

$count_foundingFather = 0;
foreach($hatbot1_array['result']['items'] as $item){
    if($item['defindex'] == 30142){
        $count_foundingFather++;
    }
}

It counts the items on how many times the items DefIndex number is found.

The JSON array that is returned from the API is huge and makes the page loading time take a really long time. Plus, having to use that chunk of code for each item makes it take even longer.

Is there a way i can make the item counting faster or do it separate from every time a person loads the page?

  • A more generic way would be looping all items, then saving the item count per `defindex` in another array (`$counts[$item['defindex']]++`) which you then could cache with memcached, apc or key/value-storage of your choice. – hank Jun 21 '14 at 11:32
  • for each item i sell on my website there is a block of code like above. The example above is the Founding Father item. There are are MANY different items that i need to tally up separately. But i don't see a different method aside from doing a ton of the bits of code above. Is there any other way? – user3729673 Jun 23 '14 at 08:29

3 Answers3

0

You could save the item count per defindex in a result array:

$counts = array();
foreach($hatbot1_array['result']['items'] as $item){
    if(isset($counts[$item['defindex']])) {
        $counts[$item['defindex']] = 1;
    } else {
        ++$counts[$item['defindex']];
    }
}

Maybe the resultant array can then be cached like hank mentioned.

the_tiger
  • 346
  • 1
  • 9
0

One way could be to filter an array with array_filter(), and then to count the resulting array.

$count_foundingFather = count( 
    array_filter( $hatbot1_array['result']['items'], function( $item ) { 
        return $item['defindex'] == 30142; 
    } 
)); 

Will that speed things up I do not know, do a benchmark test How to benchmark efficiency of PHP script. Here is a simple benchmark example without the use of external tools:

function get_time() {
    $mtime = explode( ' ', microtime() );
    return $mtime[1] + $mtime[0];
}

function speed( $start ) {
    return ( get_time() - $start );
}


$start = get_time();

// do the work

echo speed( $start );
Community
  • 1
  • 1
Danijel
  • 12,408
  • 5
  • 38
  • 54
-1

Simply use:

count($hatbot1_array['result']['items']);
Ashish
  • 332
  • 1
  • 8
  • Wont that just count the total number of items? I need to to give me a number of stock per item – user3729673 Jun 21 '14 at 11:28
  • don't you think API might have something to do it for you ?? – Ashish Jun 21 '14 at 11:31
  • the API lists every single item individually. But each item has a unique Index number to count it but i need to use that chunk of code above to count each item. But there are over 2oo types of items i need to count up individually – user3729673 Jun 21 '14 at 11:33
  • 1
    While this code may help answer the question, code only answers are not high quality. A better answer would explain what the code does, tell where to insert it, explain why this approach was taken, and link to relevant documentation. – Stephen Ostermiller Jun 21 '14 at 11:54
  • @Ashish The Steam API does NOT have something to count individual item types, and this code block will count all items, not specific items. – Andy Jun 21 '14 at 17:25
  • I am still looking for a way to shrink down the amount of code i need to use. – user3729673 Jun 23 '14 at 08:38