Jump to content

Archived

This topic is now archived and is closed to further replies.

sanfly

Arrays and Foreach

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?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
i cannot figure out a way for you to loop that data, but to access it i think you would have to do something like this

$arryname[keyname]->subkeyname;

Share this post


Link to post
Share on other sites

×

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.