shaddf Posted August 23, 2015 Share Posted August 23, 2015 i have this array: $flavors = array('Japanese' => array('hot' => 'wasabi','salty' => 'soy sauce'),'Chinese' => array('hot' => '','pepper-salty' => 'prickly ash'),'indian' => array('hot' => '','pepper-salty' => 'prickly ash'),'mexican' => array('hot' => 'soanzo','salty' => 'soy sauce'),'russian' => array('hot' => '','pepper-salty' => 'prickly ash'),'ganda' => array('hot' => '','pepper-salty' => 'prickly ash')); how can i loop through it and replace the empty values with the value of the previously filled part of say japanese ' hot for the two arrays next to it and mexican' soanzo for the next two to it using for loop Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2015 Share Posted August 23, 2015 (edited) What? I dont understand what you mean. Can you post an example of how you want the array to be filled. Edited August 23, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2015 Share Posted August 23, 2015 is this what you had in mind? $prevkey = ''; foreach ($flavors as $k1 => $arr) { foreach ($arr as $k2 => $v ) { if ($v=='') { if (isset($flavors[$prevkey][$k2])) { $flavors[$k1][$k2] = $flavors[$prevkey][$k2]; } } $prevkey = $k1; } } echo '<pre>',print_r($flavors, true),'</pre>'; gives Array ( [Japanese] => Array ( [hot] => wasabi [salty] => soy sauce ) [Chinese] => Array ( [hot] => wasabi [pepper-salty] => prickly ash ) [indian] => Array ( [hot] => wasabi [pepper-salty] => prickly ash ) [mexican] => Array ( [hot] => soanzo [salty] => soy sauce ) [russian] => Array ( [hot] => soanzo [pepper-salty] => prickly ash ) [ganda] => Array ( [hot] => soanzo [pepper-salty] => prickly ash ) ) Quote Link to comment Share on other sites More sharing options...
shaddf Posted August 24, 2015 Author Share Posted August 24, 2015 is this what you had in mind? $prevkey = ''; foreach ($flavors as $k1 => $arr) { foreach ($arr as $k2 => $v ) { if ($v=='') { if (isset($flavors[$prevkey][$k2])) { $flavors[$k1][$k2] = $flavors[$prevkey][$k2]; } } $prevkey = $k1; } } echo '<pre>',print_r($flavors, true),'</pre>'; gives Array ( [Japanese] => Array ( [hot] => wasabi [salty] => soy sauce ) [Chinese] => Array ( [hot] => wasabi [pepper-salty] => prickly ash ) [indian] => Array ( [hot] => wasabi [pepper-salty] => prickly ash ) [mexican] => Array ( [hot] => soanzo [salty] => soy sauce ) [russian] => Array ( [hot] => soanzo [pepper-salty] => prickly ash ) [ganda] => Array ( [hot] => soanzo [pepper-salty] => prickly ash ) ) $flavors[]= array('Japanese' => array('hot' => 'wasabi','salty' => 'soy sauce','sweety' => 'soy rdsauce','bitter' => 'pepper sauce'), 'Chinese' => array('hot' => '','salty' => '','sweety' => 'soy rdsauce_one','bitter' => 'pepper sauce3'), 'indian' => array('hot' => '','salty' => '','sweety' => '','bitter' => 'pepper sauce4'), 'mexican' => array('hot' => 'soanzo','salty' => 'soy sauce1','sweety' => 'soy sauceeur','bitter' => 'pepper sauce_sausage'), 'russian' => array('hot' => '','salty' => '','sweety' => '','bitter' => 'pepper sauce_pork'),'ganda' => array('hot' => '', 'salty' => '','sweety' => 'soy sauce_beef','bitter' => 'luwombo_chicken')); can you make it work for such an array. such that the empty values are filled with the content of the previous full array closest to it. say for ganda, it shpul be filled with the mexican values corresponding to each empty value, this is true for russian too. And the same setting has to be applied to those next to the full japanese array. the final array should be like this.keeping in mind that it can be bigger than this. Array ( [Japanese] => Array ( [hot] => wasabi [salty] => soy sauce [sweety] => soy rdsauce [bitter] => pepper sauce ) [Chinese] => Array ( [hot] => wasabi [salty] => soy sauce [sweety] => soy rdsauce_one [bitter] => pepper sauce3 ) [indian] => Array ( [hot] => wasabi [salty] => soy sauce [sweety] => soy rdsauce [bitter] => pepper sauce4 ) [mexican] => Array ( [hot] => soanzo [salty] => soy sauce1 [sweety] => soy sauceeur [bitter] => pepper sauce_sausage ) [russian] => Array ( [hot] => soanzo [salty] => soy sauce1 [sweety] => soy sauceeur [bitter] => pepper sauce_pork ) [ganda] => Array ( [hot] => soanzo [salty] => soy sauce1 [sweety] => soy sauce_beef [bitter] => luwombo_chicken ) ) Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 24, 2015 Solution Share Posted August 24, 2015 $prevkey = ''; foreach ($flavors as $k1 => $arr) { foreach ($arr as $k2 => $v ) { if ($v=='') { if (isset($flavors[$prevkey][$k2])) { $flavors[$k1][$k2] = $flavors[$prevkey][$k2]; } } } $prevkey = $k1; } echo '<pre>',print_r($flavors, true),'</pre>'; Sorry, I had $prevkey = $k1; in the wrong place. Quote Link to comment Share on other sites More sharing options...
shaddf Posted August 24, 2015 Author Share Posted August 24, 2015 $prevkey = ''; foreach ($flavors as $k1 => $arr) { foreach ($arr as $k2 => $v ) { if ($v=='') { if (isset($flavors[$prevkey][$k2])) { $flavors[$k1][$k2] = $flavors[$prevkey][$k2]; } } } $prevkey = $k1; } echo '<pre>',print_r($flavors, true),'</pre>'; Sorry, I had $prevkey = $k1; in the wrong place. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.