rockinaway Posted February 14, 2008 Share Posted February 14, 2008 // Loop within the values array for ($i = 0; $i < count($array); $i++) { // Now we go through the keys foreach ($array[$i] as $key => $value) { $keys[] = '{X_'.$key.'}'; $values[] = $value; } // Assign values within the loop source and add to data variable $data .= str_replace($keys, $values, $loop_source); } I have this for and foreach setup so that I can loop and replace through some values. The $array variable is a multi-dimensioned array...(i think!).. It is like: $array = array ( 'test' => array ( 'test2' => 'yes' ), 'test2' => array ( 'test3' => 'yes' ), ); That is just an example. However I am having problems looping through. I always get this error: Warning: Invalid argument supplied for foreach() What is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/ Share on other sites More sharing options...
aschk Posted February 14, 2008 Share Posted February 14, 2008 It's probably not a multi-dimensional array. Do: <?php echo "<pre>"; print_r($array); echo "</pre>"; ?> to verify that it is. Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-466969 Share on other sites More sharing options...
Psycho Posted February 14, 2008 Share Posted February 14, 2008 Based upon the array at the end of your post the problem is that you are trying to reference those inner arrays with numerical values using the $i variable. There is NO $array[0] value so your foreach loop is failing. You need to reference those inner arrays using their proper index values: <?php // Loop within the values array foreach ($array as $subarray) { // Now we go through the keys foreach ($subarray as $key => $value) { $keys[] = '{X_'.$key.'}'; $values[] = $value; } // Assign values within the loop source and add to data variable $data .= str_replace($keys, $values, $loop_source); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-466971 Share on other sites More sharing options...
rockinaway Posted February 14, 2008 Author Share Posted February 14, 2008 Doing what aschk said, I got repeated 'Array' on the page mjdamato - I did what you said. However I still get the same error for the line: foreach ($array as $subarray) What can be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-466974 Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 You don't have any numerically indexed entries in your array, so $array[0] is meaningless. Use two foreach loops: <?php $array = array ( 'test' => array ( 'test2' => 'yes' ), 'test2' => array ( 'test3' => 'yes' ), ); $keys = array(); $values = array(); foreach ($array as $ary) foreach ($ary as $k => $v) { $keys[] = '{X_'.$k.'}'; $values[] = $v; } echo '<pre>' . print_r($array,true) . '</pre>'; echo '<pre>' . print_r($keys,true) . '</pre>'; echo '<pre>' . print_r($values,true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-466980 Share on other sites More sharing options...
rockinaway Posted February 14, 2008 Author Share Posted February 14, 2008 Doing what aschk said, I got repeated 'Array' on the page mjdamato - I did what you said. However I still get the same error for the line: foreach ($array as $subarray) What can be wrong? ^^^ That is what mjdamato suggested.. however problem resides... Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-466982 Share on other sites More sharing options...
rockinaway Posted February 14, 2008 Author Share Posted February 14, 2008 Ok, I am past that. But now, when I echo out the result. The same value has been replaced for each one. So instead of test1 and test2, test1 and test1 again.. what could be causing the overwriting? Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-467001 Share on other sites More sharing options...
Psycho Posted February 14, 2008 Share Posted February 14, 2008 Because your: $data .= str_replace($keys, $values, $loop_source); is inside the loop. Move it after the loops. Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-467165 Share on other sites More sharing options...
rockinaway Posted February 14, 2008 Author Share Posted February 14, 2008 But then it only shows one of the results.. no more... Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-467195 Share on other sites More sharing options...
Psycho Posted February 14, 2008 Share Posted February 14, 2008 Really? that makes no sense, because your loop is continually adding to the $keys and $values arrays. The way you had it before would first process all the values from the first array - then it would process all the values from the first & second arrays. You really haven't provided enough information to help you. Quote Link to comment https://forums.phpfreaks.com/topic/91111-for-and-foreach-not-friends/#findComment-467220 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.