Run array through foreach.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Run array through foreach.

Post by donny »

hello,

i need help writing out a foreach that will run through an array and take info from it also.
i need to run through each array under every ordernumber. some order numbers only have 1 array under it, some have more.
i also need to know how to set variables in the foreach using the data in the arrays such as info1, info2.


this is what my arrays look like

Code: Select all

Array
(
    [ORDERNUMBER] => Array
        (
            [0] => Array
                (
                    [0] => dataforinfo1
                    [info1] => dataforinfo1
                    [1] => dataforinfo2
                    [info2] => dataforinfo2
                    [5] => dataforformnumber
                    [formnumber] => dataforformnumber

                )

            [1] => Array
                (
                    [0] => dataforinfo1
                    [info1] => dataforinfo1
                    [1] => dataforinfo2
                    [info2] => dataforinfo2
                    [5] => dataforformnumber
                    [formnumber] => dataforformnumber
                )
        )

    [ORDERNUMBER] => Array
        (
            [0] => Array
                (
                    [0] => dataforinfo1
                    [info1] => dataforinfo1
                    [1] => dataforinfo2
                    [info2] => dataforinfo2
                    [5] => dataforformnumber
                    [formnumber] => dataforformnumber
                )

        )

)
my foreach should look something like this.

Code: Select all

foreach (each array under ordernumber  seperatly) {

//set variables
$info1 = dataforinfo1;
$info2 = dataforinfo2;
$formnumber = dataforformnumber;


//OTHER CODING WILL BE HERE

}

thanks a lot!!!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Run array through foreach.

Post by Celauran »

Nested foreach loops.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Run array through foreach.

Post by donny »

thank you i was able to figure it out by looking up nested foreach loops.

so in the end this is the code i needed

Code: Select all

<?php
include 'dblogin.php';
$select_list = $_POST['selected'];
foreach ($select_list as $key => $value) {
        $select_list[$key] = "'" . $value . "'";
}
$list = implode(', ', $select_list);

$query = "SELECT * FROM `rawdata` WHERE `ordernumber` IN ($list)";
$result = mysql_query($query) or die(mysql_error());

$ordercid = array();
while ($row = mysql_fetch_array($result)) {
        $ordercid[$row['ordernumber']][] = $row;
}

foreach($ordercid as $ordernumber) {
foreach($ordernumber as $info) {
include '/'.$info['type'].'script.php';
}
}
?>
thanks for all your help
Post Reply