Jump to content

For and foreach not friends?


rockinaway

Recommended Posts

                                // 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?

Link to comment
Share on other sites

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);
}
?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

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.