You will need to loop through each item in your result $schema
and search for the defindexes you are looking for.
For example, if you want to count all of your metal, you'll be looking for 5000
, 5001
, and 5002
.
$metal_array = array(5000, 5001, 5002);
foreach ($schema as $item)
{
if (in_array($metal_array, $item->defindex)
{
// Do something for the $item. Presumably, you'll add these up
// So that you can get a total metal count (remember that refined is worth 9 scrap
// and reclaimed is worth 3)
}
}
Edit:
To answer some of the questions in your comment:
$item->defindex
is referring to the defindex
of the current $item
object in your schema. You are performing object iteration in this foreach loop. Each item
is being iterated over in the foreach
loop. So, on the first pass through the loop, $item
will look like this:
{
"id": 1828947688,
"original_id": 1176490973,
"defindex": 5009,
"level": 1,
"quality": 6,
"inventory": 2147483650,
"quantity": 1,
"origin": 4
}
You can access any of these values by doing $item->ATTRIBUTENAME
(ie. $item->quality
or $item->origin
). On this pass through the loop, it will check if $item->defindex
, which equals 5009
is in the $metal_array
, which contains 5000
, 5001, and
5002`. It does not, so the if block does not execute.
As for what you'd do in that loop, I'd probably modify the code slightly to do something like this:
$metal_array = array(5000, 5001, 5002);
$total_metal = 0;
foreach ($schema as $item)
{
if (in_array($metal_array, $item->defindex)
{
switch ($item->defindex)
{
case 5000:
$total_metal++; // or $total_metal += 1;
break;
case 5001:
$total_metal += 3;
break;
case 5002:
$total_metal += 9;
break;
}
}
}
At the end of this block, you will have a value in $total_metal
that equals the scrap value of all of your metal.
This block uses the switch statement to determine how much a particular item is worth. The break statement within each case
prevents the logic from "falling through" to the next option.
For example, if the break
was not added to the end of the case 5000
block of code and an $item->defindex
equaled 5000
, it would add 1
and then 3
. This is not the answer you want. It is, however, a valid option to consider in certain circumstances.
switch ($item->defindex)
{
case 5000:
$total_metal++; // or $total_metal += 1;
case 5001:
$total_metal += 3;
break;
}
Finally, if you are new to PHP, I recommend taking a look at the documentation: http://php.net/manual/en/index.php