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?

2
  • I believe more context is required here, a simple solution for what you have provided would be impossible Commented May 18, 2010 at 14:23
  • I don't think there's a standard way of performing this exact task. Just give it a go. Commented May 18, 2010 at 14:24

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hi! im getting this: Array ( [Los secretos del poder] => Array ( [0] => Argentina [1] => Mexico [2] => Uruguay ) [La sombra del poder] => Array ( [0] => Spain ) ) How can i get the movie title inside a [title] index??
Great@! thanks, its working.. I wonder if there is a way to simplify those two foreachs in one thing? sorry but im new in php :)
both steps are needed. first steps groups the items by title. second step creates the array in the format you want.
1

Try with:

$input  = array( /* your data */ );
$output = $tmp = array();

foreach ( $input as $v ) {
  if ( !isset($tmp[$v['title']]) {
    $tmp[$v['title']] = array();
  }

  // here you can check if your counry is spanish speaking
  if ( !in_array($v['country'], $spanishSpeakingCountries) ) {
    continue;
  }

  $tmp[$v['title']][] = $v['country'];
}

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

$output; // your output data

1 Comment

im getting a parse error here: foreach ( $input as $v ) { if ( !isset($tmp[$v['title']]) {
1
foreach($yourarray as $data)
{
    $newlist[$data['title']][] = $data['country'];
}

This would give you

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

1 Comment

you forgot to check if the country was spanish speaking!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.