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 813227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:19:32+00:00 2026-05-15T01:19:32+00:00

I don’t know how to get it to work the best way. I need

  • 0

I don’t know how to get it to work the best way.

I need to loop through an array like the one below. I need to check if the [country] index is equal to a Spanish speaking country (lot of countries I predefine) and then get those [title] indexes of the correspondent country, check for duplicates and create new more compact and simplified array.

The original array:

Array
(
    [0] => Array
        (
            [title] => Jeux de pouvoir 
            [country] => France
        )

    [1] => Array
        (
            [title] => Los secretos del poder
            [country] => Argentina
        )

    [2] => Array
        (
            [title] => Los secretos del poder 
            [country] => Mexico
        )

    [3] => Array
        (
            [title] => El poder secreto 
            [country] => Uruguay
        )
)
goes on and on....

To help you understand, the final result I need to get looks something like this:

Array (
    [0] => Array
        (
            [title] => Los secretos del poder
            [country] => Argentina, Mexico
        )
    [1] => Array
        (
            [title] => El poder secreto
            [country] => Uruguay
        ) 
)

As you can see, when there is the same title for lot of countries the array gets simplified by adding those countries to the same [country] index of the corresponding [title].

How would you do it?

  • 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-15T01:19:33+00:00Added an answer on May 15, 2026 at 1:19 am

    assuming $spanish_countries is an array of spanish speaking countries…

    foreach ( $array as $a ) {
        if ( in_array($a['country'], $spanish_countries) ) {
            $final_array[$a['title']][] = $a['country'];
        }
    }
    

    this will result in a different array at the end but would be trivial to get to your format


    Edit for comment

    foreach ( $final_array as $k => $v ) {
      $r[] = array(
        'title'   => $k,
        'country' => implode(', ', $v)
      );
    }
    print_r($r);
    

    youll want better variable names, but this will work

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

Sidebar

Related Questions

I don't know if this has been asked before, but what i'd like to
I don't have much PHP experience and I want to know how to best
I don't know when to add to a dataset a tableadapter or a query
I don't edit CSS very often, and almost every time I need to go
I don't know if anyone has seen this issue before but I'm just stumped.
Don't get me wrong, I want them to get saved. But I always thought
Don't know if this has been answered before. Have custom routes to users. If
I don't like Jackson. I want to use ajax but with Google Gson. So
I don't know how to explain to you the trouble by using faux columns.
I don't know why, but there is a problem I'm encountering with $.parseJSON when

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.