Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
PHP : cannot access array elements after foreach loop

I am printing array elements

foreach($subcategories as $sucategory){
  print_r($sucategory);
  echo $subcategory[2];
  echo $subcategory[catid];
  echo $subcategory['catid'];
  echo $subcategory["catid"];
  echo $subcategory{"catid"};
  die();
}

The above code prints only the value of print_r($subcategory) but it didn't echo the value of catid in $subcategory..

Array
(
    [id] => 5
    [name] => 1
    [catid] => 10
    [status] => Y
)

and

echo $subcategories[0]['catid']; //prints 10 correctly
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have an error there. You use $sucategory and $subcategory, different names!

Copy/Paste this and it will work:

foreach($subcategories as $subcategory){
  print_r($subcategory);
  echo $subcategory["catid"];
  die();
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!