Tom
Tom

Reputation: 561

Splitting up a string - php

I have an array called $result, which is shown below:

array(1) 
    { ["response"]=> array(3) 
        { [0]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(noun)" 
                ["synonyms"]=> string(27) "chelonian|chelonian reptile" } 
            } 
          [1]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(verb)" 
                  ["synonyms"]=> string(57) "capsize|turn turtle|overturn|turn over|tip over|tump over" } 
            } 
          [2]=> array(1) 
            { ["list"]=> array(2) 
                { ["category"]=> string(6) "(verb)" 
                  ["synonyms"]=> string(29) "hunt|run|hunt down|track down" } 
            } 
         } 
    }

I am trying to access the ["synonyms"] element, and split each word and store it in its own string, or perhaps an array of all the words. You can see the words are separated by the | symbol.

I have tried the following code, which did not work (results did not display, so explode did not work) :

$i=0;

foreach ($result["response"] as $value) 
{ 
 foreach ($value["list"]["synonyms"] as $temp)
  {
  $alternative[$i] = explode ("|", $temp);
  $i++;
  }

}

//OUTPUT THE RESULTS

$j=0;

foreach ($alternative as $echoalternative)
{
echo $j.": ".$echoalternative;
$j++;
}

Any ideas? Thanks guys.

Upvotes: 1

Views: 152

Answers (3)

Expedito
Expedito

Reputation: 7795

To create an array of single dimensional arrays (given that you have three groups in your original array), you can do the following:

$out = array();
foreach ($arr['response'] as $key => $value){
    $syns = explode('|', $value['list']['synonyms']);
    foreach ($syns as $key2 => $value2){
        $out[$key][] = $value2;
    }
}

To access the single dimensional array for the group of synonyms with index 0, just do the following:

var_dump($out[0]);

Array(
    [0] => chelonian
    [1] => chelonian reptile
)

If you just want to display the synonyms, you can do something like this:

foreach ($arr['response'] as $key => $value){
    $syns = explode('|', $value['list']['synonyms']);
    foreach ($syns as $key2 => $value2){
        echo $value2.', ';
    }
    echo "<br />";
}

Output:

chelonian, chelonian reptile,
capsize, turn turtle, overturn, turn over, tip over, tump over,
hunt, run, hunt down, track down, 

However, if you want to include that in the original array, you can do this:

array_walk_recursive($arr, function (&$e, $k){
    if (preg_match('#[\w\|]+#', $e)){
        $e = explode('|', $e);
    }
});
var_dump($arr);

Output:

Array(
    [response] => Array
            [0] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (noun)
                                )
                            [synonyms] => Array(
                                    [0] => chelonian
                                    [1] => chelonian reptile
                                )
                        )
                )
            [1] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (verb)
                                )
                            [synonyms] => Array(
                                    [0] => capsize
                                    [1] => turn turtle
                                    [2] => overturn
                                    [3] => turn over
                                    [4] => tip over
                                    [5] => tump over
                                )
                        )
                )
            [2] => Array(
                    [list] => Array(
                            [category] => Array(
                                    [0] => (verb)
                                )
                            [synonyms] => Array(
                                    [0] => hunt
                                    [1] => run
                                    [2] => hunt down
                                    [3] => track down
                                )
                        )
                )
        )
)

Upvotes: 3

nvn
nvn

Reputation: 52

The following refactored code should resolve the issue

$i=0;

foreach ($result["response"] as $value) 
{ 
// print_r($value);
$temp = $value["list"]["synonyms"];
// echo $temp;
 // foreach ($value["list"]["synonyms"] as $temp)
  // {
  $alternative[$i] = explode ("|", $temp);
  $i++;
  // }

}

//OUTPUT THE RESULTS

$j=0;

foreach ($alternative as $echoalternative)
{
print_r($echoalternative);
echo $j.": ".$echoalternative;
$j++;
}

Upvotes: 2

StephenTG
StephenTG

Reputation: 2647

You're trying to iterate over the string in your interior foreach. Try

foreach ($result["response"] as $value) 
{ 

  $alternative[$i] = explode ("|", $value["list"]["synonyms"]);
  $i++;


}

Upvotes: 3

Related Questions