To get the key of an item inside a foreach loop in PHP, you can use the following syntax:
phpforeach($the_main_array as $key => $item)
{
// $key will contain the key of the current item
}
In your case, the code would look like this:
phpforeach($the_main_array as $key => $item)
{
// $key will be 952, 91, 83
}
This way, $key
will contain the key of the current item in each iteration of the loop.
المزيد من المعلومات
بالطبع! في PHP، عند استخدام foreach
للتكرار على عناصر المصفوفة، يمكنك استخدام السطر foreach($array as $key => $value)
للوصول إلى المفتاح وقيمة كل عنصر في المصفوفة. في هذا السياق، يكون $key
هو المفتاح و $value
هو القيمة لكل عنصر.
المفتاح يكون عادةً رقميًا إذا كانت المصفوفة تستخدم مفاتيحًا رقمية، كما هو الحال في المثال الذي ذكرته. ويمكنك الآن الوصول إلى المفتاح داخل الحلقة foreach
باستخدام $key
.