I create an array like this:
$table = Array();
array_push($table, Array('item' => 1, 'storage' = 1, 'qtd' = 0) );
array_push($table, Array('item' => 1, 'storage' = 2, 'qtd' = 4) );
array_push($table, Array('item' => 2, 'storage' = 1, 'qtd' = 78) );
array_push($table, Array('item' => 3, 'storage' = 2, 'qtd' = 10) );
I need to search if i have some item in some storage.. For example in a sql query i do like "... where item = 1 and storage = 2"
How can i search that way in the array, to get the "qtd" value?
Thanks!
$table = Array();
//populate $table array this way
$table[1]=array('item' => 1, 'storage' = 1, 'qtd' = 0);
$table[1]=array('item' => 1, 'storage' = 2, 'qtd' = 4);
$table[2]=array('item' => 2, 'storage' = 1, 'qtd' = 78);
$table[3]=array('item' => 3, 'storage' = 2, 'qtd' = 10);
//to prevent over-writting use in_array() function
if(!in_array(1 /*item*/ ,array_keys($table))){
$table[1]=array('item' => 1, 'storage' = 1, 'qtd' = 0);
}
//the following will not be inserted as we have already a value at index=1
if(!in_array(1,array_keys($table)),){
$table[1]=array('item' => 1, 'storage' = 2, 'qtd' = 4);
}
EDITED**** to get highest max index in $table
array
$current_primary_key = max(array_keys($table));
$next_primary_key = $current_primary_key + 1;
Using the suggestion of alamnaryab, i change to something like this, that works for me:
$table = Array();
$table[1][1]=array('qtd' => 0);
$table[1][2]=array('qtd' => 4);
$table[2][1]=array('qtd' => 78);
$table[3][2]=array('qtd' => 10);
if (!isset($table[1][2]))
{
echo "Dosent Exists";
}
else
{
echo "exists. qtd: " . $table[1][2]['qtd'];
}