php - Foreach in Multi-Dimensional Array -


i'm trying practice/learn foreach loops in php. understand basic foreach. struggle multi-dimensionals.

i have array test out:

$data = array(         array('new york', 'new york city'),         array('hotel', 'lodging','motel'),         array('cheap')     ); 

what loop through each , fine every possible combination & assign it's own array (will later displayed columns).

first column ('new york', 'new york city') second column ('new york hotel', 'new york lodging', 'new york motel') third column ('new york city hotel', 'new york city lodging', 'new york city motel') fourth column ('new york hotel cheap', 'new york lodging cheap', 'new york motel cheap') fifth column ('new york city hotel cheap', 'new york city lodging cheap', 'new york city motel cheap') 

how accomplish this? i've tried couple things, haven't gotten close.

update end goal have individual able take list of items, , try find possible keywords. in identifying different seo keywords can consider using. data array may have 2 sub arrays, other times may have 3 or 4. need bit dynamic.

1st solution attempt

public function recurseful($start, $args) {     if (is_array($args))         {             foreach ($args[0] $value)             {                 $this->output[] = trim("{$start} {$value}");                 if (count($args) > 1)                 {                     $this->recurseful($value, array_slice($args, 1));                 }              }         }     return; } 

however, returns of words in single array. not meet requirements of i'm looking for.

what need create function you, recursively, like....

// function building recursive tree arrays function recursive($array) {     foreach($array $key => $value) {         if(is_array($value)) {             // if array, run again             recursive($value);         } else {             // values here         }     }     // return result set here }  $data = array(         array('key', 'key2'),         array('word', 'word2','word3'),         array(array('more','levels','than','you','need'), 'then','some','more')     );  $result = recursive($data); 

it's rough, depending on you're doing, need add process out total final result.

basically, keep running regardless of how many depths array is, or sequence of array structure. need ensure sending (returning) results of each sweep of function, depend on each level.

it should on way though.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -