From what I have been able to observe, the objective is that for each product that you have in your store, you want to show the information of the stock (total amount of products) that is obtained through the api. They handle a series of inconveniences on whether to show the result in an html file or in a php file.
If it's php, the script can run faster by calling a function that counts the items, for example you could have a function like this:
function contar_item($param, $array){
$count = 0;
foreach($myarray['result']['items'] as $item){
if($item['defindex'] == $param){
$count++;
}
}
return $count;
}
And when the php script is running, call that function, for example:
<?php
for ($i=0; $i<$array_local; $i++){
echo "<a href='#' class='item' data-id='".$i."'> ".contar_item($i,$array)." </a>";
}
?>
Note that $ array_local can be an integer, or an array. If it is an array you can use the foreach and use the indexes for the parameters.
If it is html, you must make an ajax call for each item and pass as a parameter the product identifier (or the value that represents the number "5021" in your code). If this is the case, the foreach must do from javascript, for example using jquery, you would have something like this:
$( "a.item" ).each(function() {
var id_item = $(this).data('item');
var node = $(this);
$.ajax({
type:"POST",
url:"llamadaAPI.php",
data:"item="+id_item,
success: function(data){$(node).html(data);}
});
});
calledAPI.php will receive a POST called item, which will have to be used as a search parameter. At the end you should print the counter.
This is assuming that the api does not accept more filtering parameters, as I have worked with this type of responses, you can send more filtering parameters. Something like this:
link
Where parametro1 can be the catalog, and parameter2 the id of the product.
In this last case, if you want to make the answer less heavy, I recommend that you section the total of products, and whether you are loading more automatically when doing a scroll, put a "load more" button, or use pagination .