Jump to content

Arrays and Foreach


sanfly

Recommended Posts

Lets say I have the following array

[code]Array (
[b_name] => Array ( [0] => Naquadah [1] => Remote [2] => These Four Walls )
[b_web] => Array ( [0] => www.naquadah.co.nz [1] => www.remote.net.nz [2] => www.thesefourwallsband.com )
) [/code]

I want to use a foreach loop to get the data out, but im not quite sure how to execute it properly. Below is how I imagined the loop would go, but obviously not working

[code]// Insert the bands into the database
    $bands = array();
    $bands['b_name'] = $b_name; // The Band names in an array
    $bands['b_web'] = $b_web;  // The Band websites in the array
    
    foreach($bands as $val){
            $b_name = addslashes($val['b_name']);
            $b_web = $val['b_web'];
        
            if($b_name){
            //$b_web = addslashes(validateWeb($b_web));
            // insert goes here
            }
        
    }[/code]

Can anyone help me figure out how it should really go?
Link to comment
Share on other sites

Well, first of all, you need to look at your design. What you're doing with a single array doesn't really make sense. You need to use either a 2 dimensional array or use 2 arrays.

I'm not sure why you're sticking them into an array before you dump them into the database either. If that's coming from some multi-listing form, you could easily setup the form to pass an array to the processing page.

I'm also not sure if a foreach is what you really want to do here... a simple for loop would get the job done.

But here is the syntax for a foreach loop:
[code]foreach($arrayname as $key => $val)
{
     // $key is the index of the array
     // $val is the value at that index
}[/code]
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.