tags:

views:

752

answers:

3

Hi, I have a multidimensinal array.. Something like this for example:

Array (
    [0] => Array
        (
            [title] => Star Trek - Viaje a las estrellas
            [country] => Venezuela, Spain, long title, poster title
        )

    [1] => Array
        (
            [title] => Viaje a Las Estrellas
            [country] => Venezuela
        )
)

I want to get te text between commas from [country] and insert each element into separate indexes, for example:

Array (
    [0] => Array
        (
            [title] => Star Trek - Viaje a las estrellas
            [country] => [0] => Venezuela
                         [1] => Spain
                         [2] => long title
                         [3] => poster title
        )

    [1] => Array
        (
            [title] => Viaje a Las Estrellas
            [country] => Venezuela
        )
)

Probably the array layout is incorrect but I just want to explain to you what I need to do. Note that not always [country] contains elements separated by commas, sometimes is just one single element.

How can I do it??

Thanks!

+4  A: 

Try using explode() on the country element. You can use a separator of ", ", since these are comma-separated values.

One way to do it (which is similar to how others have suggested) would be:

// Assuming that $data contains your multidimensional array...
for ($i = 0; $i < count($data); $i++)
{
    if (strstr($data[$i]['country'], ', '))
    {
     $data[$i]['country'] = explode(', ', $data[$i]['country']);
    }
}

Also, note that you don't really need to use strpos()strstr() works perfectly here.

htw
-1 for lack of code and lack of strpos function.
St. John Johnson
Lack of code, sure, but strpos isn't necessary.
Tony k
strpos would be better, no + from me.
OIS
+1  A: 

You could use the preg_split function and a regular expression to split the string:

foreach ($array as $key => $item) {
    if (strpos($item['country'], ',') !== false) {  // check if string contains a comma
        $array[$key]['country'] = preg_split('/,\s*/', $item['country']);
    }
}
Gumbo
Why use preg_split when there's a perfectly good explode() function available?
Paolo Bergantino
@Paolo Bergantino: To remove possible space characters too.
Gumbo
Why not just include the space in the delimiter for explode()? preg_split() seems kind of like overkill here...
htw
-1 from me, sorry. I must agree with Paolo on this one.
Flavius
@htw: I don’t know where the values to process come from and whether the comma is always followed by a single space character. Using a regular expression can cover this uncertainty.
Gumbo
By the way: Why does everyone have such a fundamental aversion to regular expressions? Regular expressions are not in general evil.
Gumbo
Gumbo: use the right tool for the job
OIS
@htw, what happens when one record has no spaces, and the next record has multiple spaces? The preg_split will work with both 'a,b' and 'a, b'.
Zoredache
Sure, but I'd also say that it depends on the task at hand. If it's data that is entered manually by a user or comes from a source that the programmer doesn't ahve control over, then yes, preg_split() would be better. But otherwise, explode() fits the bill perfectly. In other words, as OIS said, use the right tool for the job.
htw
A: 
$a = Array (
    0 => Array
        (
            "title" => "Star Trek - Viaje a las estrellas",
            "country" => "Venezuela, Spain, long title, poster title"
        ),

    1 => Array
        (
            "title" => "Viaje a Las Estrellas",
            "country" => "Venezuela"
        )
);
$res = array();    
    foreach($a as $k => $v){
        foreach($v as $key => $value){
            switch($key){
                case "country":                                
                    $r = split(",", $value); 
                    foreach($r as $index => $val){
               $res[$k][$key][$index] = trim($val);
                    }
                  break;
                default:
                    $res[$k][$key] = $value;
                break;
            }
        }
    }

    print_r($res);

output:

Array
(
    [0] => Array
        (
            [title] => Star Trek - Viaje a las estrellas
            [country] => Array
                (
                    [0] => Venezuela
                    [1] => Spain
                    [2] => long title
                    [3] => poster title
                )
        )
    [1] => Array
        (
            [title] => Viaje a Las Estrellas
            [country] => Array
                (
                    [0] => Venezuela
                )
        )
)
Tom Schaefer
Note that, since you are exploding the string using just ',' as a delimiter, there are leading spaces in three of the values in the country array.
htw
This code is pretty bad ...
OIS