Jump to content

Multi-Dimensional Arrays. [Advanced Question]


ErikL

Recommended Posts

Hello, here is the PHP situation I'm in. I'll type it out in pseudo-ish code first.

<?php
            $multi_array = array(
                 array("cats", "dogs", "sheep", "walruses"),
                 array("jazz", "larva", "pencils", "derp"),
                 array("to", "much")
            );
            $string = array("the", "cats", "ate", "too", "many", "pencils");
            $count = count($string);
            for($i = 0; $i < $count; ++$i) {
                  if($string[$i] IN ARRAY $multi_array) {
                            THEN replace $string[$i] with a random word in the sub array of the multi-array
                             for example, if $string[$i] is cat, then it would replace it with any of the words in the sub array of multi-array, so it could be "cats", "dogs", "sheep", or "walruses"
                  }
           }
?>

 

 

I have no idea how to do this and tried thinking of ways for an hour and a half.  :-[

Link to comment
Share on other sites

Certainly, I have a multi-dimensional array and a normal array.

 

I want to loop threw the array and if the normal array has any elements that are equal to any elements of the multi-dimensional array, then replace it with a random element of the sub array of the multi-dimensional array.

 

I hope that clarifies a few things.

Link to comment
Share on other sites

One question, if the element that is being compared matches an element in the multi-dimensional array, where does the replacement element come from? Any one of the sub-arrays or the sub-array which contains the match?

 

Ken

 

The replacement would come from the array of which the key matched (randomly).

 

for example if the multi-array is: array("chocolate", "cinnamon", "strudels");

and the thing being compared is "chocolate" then "chocolate" could be replaced with "cinnamon", "strudels", or "chocolate"

Link to comment
Share on other sites

try

<?php
$multi_array = array(
                 array("cats", "dogs", "sheep", "walruses"),
                 array("jazz", "larva", "pencils", "derp"),
                 array("to", "much")
            );
$string = array("the", "cats", "ate", "too", "many", "pencils");
$count = count($string);
foreach($string as $i => $word) {
    foreach ($multi_array as $ar){
        if(in_array($word, $ar)){
            $string[$i] = $ar[array_rand($ar)];
            break;
        }
    }
}
print_r($string);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.