Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:58:25+00:00 2026-05-25T05:58:25+00:00

Is there a way to foreach() through one array based on a matching value

  • 0

Is there a way to foreach() through one array based on a matching value with a different key in another array? In this example, I have a category array ($cat_data) with cat_id as a key and an image array ($img_data) with category_id as a key.

Array (
   [0] => Array (
        [cat_id] => 1
        [cat_name] => Category 1
   )
   [1] => Array (
        [cat_id] => 2
        [cat_name] => Category 2
   )
)
Array (
   [0] => Array (
        [img_id] => 2
        [img_name] => demo1.jpg
        [img_label] => Demo 1
        [category_id] => 2
        [img_order] => 1
   )
   [1] => Array (
        [img_id] => 3
        [img_name] => demo2.jpg
        [img_label] => Demo 2
        [category_id] => 2
        [img_order] => 2
   )
   [2] => Array (
        [img_id] => 4
        [img_name] => demo3.jpg
        [img_label] => Demo 3
        [category_id] => 1
        [img_order] => 1
   )
)

What I want is to output my display so it looks like the following:

Category 1
  demo3.jpg

Category 2
  demo1.jpg
  demo2.jpg

Since I’m really not great at fully grasping arrays, I thought I’d try Stack, and I haven’t been able to find an answer to my question, partially because I’m not sure what to ask for precisely. Any help??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:58:25+00:00Added an answer on May 25, 2026 at 5:58 am

    The naïve way:

    foreach ($cat_data as $cat) {
        echo $cat['cat_name'];
    
        foreach ($img_data as $img) {
            if ($img['category_id'] != $cat['cat_id']) {
                continue;
            }
    
            echo $img['img_name'];
        }
    }
    

    This is rather inefficient, since it loops through the $imgs array several times, but easy and works.

    More efficient:

    $images = array();
    foreach ($img_data as $img) {
        $images[$img['category_id']][] = $img;
    }
    
    foreach ($cat_data as $cat) {
        echo $cat['cat_name'];
    
        if (isset($images[$cat['cat_id']])) {
            foreach ($images[$cat['cat_id']] as $img) {
                echo $img['img_name'];
            }
        }
    }
    

    This first groups all images by category into a new array, which you can then loop over directly once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any way to write this foreach in linq or another better way,
Is there a way to iterate (through foreach preferably) over a collection using reflection?
Is there a way to use a foreach loop to iterate through a collection
Is there a way to have different Global Configurations for each section of articles?
Is there a better way in PHP to access this array in groups of
Is there a way to do foreach style iteration over lined up enumerables in
Within a Foreach loop in SSIS is there a way that if a task
Is there a way to do the following using LINQ? foreach (var c in
Is there a way to iterate ValueCollection using straight for loop? (not foreach) e.g.
I have a JUnit 4 test that loops through an array of test data:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.