lets suppose the example I am using is the following
$fields = array('Name','Age','Grade');
$studentsDictionary = array(array('Name'=>'Mark','Age'=>15,'Grade'=>19),
array('Name'=>'John','Age'=>15,'Grade'=>11),
array('Name'=>'Tom','Age'=>15,'Grade'=>10));
for($i =0;$i<sizeof($studentsDictionary);$i++)
{
for($j=0;$j<sizeof($fields);$j++)
{
echo $studentsDictionary[$j][$fields[$i]].' ';
echo array_keys($studentsDictionary)[$j][$i].' ';
}
echo $nl;
}
I have 2 way to access the key of the associative array one is creating an array which contains the string keys for said associative array and the other I found was using array_keys(arrayName)[index][key_index] (like using [i][j] but backwards indexing…)
so every time I use the 2nd method it shows
Notice: Trying to access array offset on value of type int in
C:\xampp\htdocs\phptest\index.php on line 75
can I disable such notice for this case?
Notice means a bug. So better to fix the bug.
Your first method is incorrect. $i and $j should be shifted.
Second method is also incorrect. array_keys method works on associative arrays. You have applied array_keys to a normal array.
Correct code is,
$fields = array('Name','Age','Grade');
$studentsDictionary = array(array('Name'=>'Mark','Age'=>15,'Grade'=>19),
array('Name'=>'John','Age'=>15,'Grade'=>11),
array('Name'=>'Tom','Age'=>15,'Grade'=>10));
for($i =0;$i<sizeof($studentsDictionary);$i++)
{
for($j=0;$j<sizeof($fields);$j++)
{
echo $studentsDictionary[$i][$fields[$j]].' ';
echo array_keys($studentsDictionary[$i])[$j].' ';
}
echo $nl;
}