array messed up

Discussion in 'PHP' started by stats, Dec 26, 2007.

  1. #1
    Hi

    I'm a bit lame in regards to the array .. so could anyone help me figure this out plz.

    from some calculations i'm getting back a set of results which i have to insert into MySQL DB. I don't want to insert the results at the same time as the calculation takes place because it is a long calculation and i don't want to keep my table incomplete for that long time.

    so what i'm trying to do is collect all the results in an array as the calculation goes, and then once it is ended i push all the results to DB very fast.

    so .. suppose i have something like this

    $myarray = array();
    while ($i =0; $i<= 10; $i++) { // make some funky calculations and put the results for $a, $b, $c into $myarray
    $a = $i;
    $b = 2*$i;
    $c = 3*$i;
    $myarray[] = array("a" => $a, "b" => $b, "c" => $c);
    }
    
    for ($n = 0; $n <= sizeof($myarray); $n++) { // read $myarray and transfer it to a mysql DB
    $a = $myarray[$n][a];
    $b = $myarray[$n][b];
    $c = $myarray[$n][c];
    mysql_query("INSERT INTO `mydb` (`a`, `b`, `c`) VALUES ('$a', '$b', '$c')");
    }
    PHP:
    so basically my first cycle does the calculation (a heavy one) and puts the results into $myarray. And then the second cycle just reads that array and puts whatever it finds there into my DB.

    I believe i have a misunderstanding about multidimensional arrays because whatever i just wrote above doesn't work .. actually i'm getting a ZERO for the size of $myarray ..

    so .. how should i enter those values into my multidimensional array ?

    Thanks
     
    stats, Dec 26, 2007 IP
  2. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #2
    
    			$myarray = array();
    			for($i=0; $i<= 10; $i++) {
    			$a = $i;
    			$b = 2*$i;
    			$c = 3*$i;
    			$myarray[] = array("a" => $a, "b" => $b, "c" => $c);
    			}
    			$arraysize = sizeof($myarray)-1;
    			for ($n = 0; $n <= $arraysize; $n++) { // read $myarray and transfer it to a mysql DB
    			$a = $myarray[$n][a];
    			$b = $myarray[$n][b];
    			$c = $myarray[$n][c];
    			echo "INSERT INTO `mydb` (`a`, `b`, `c`) VALUES ('$a', '$b', '$c')<br>";
    }
    
    PHP:
     
    falcondriver, Dec 26, 2007 IP
  3. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    kinda didn't catch .. what's the main difference ?
     
    stats, Dec 26, 2007 IP
  4. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #4
    first block starts with for() instead of while(), and the second for-loop goes only until sizeof($myarray)-1. thats because the array elements go from 0...9 here.

    you could also use
    
    foreach($myarray as $data) {
    echo $data[0]."<br>";
    echo $data[1]."<br>";
    echo $data[2]."<br>";
    }
    PHP:
    foreach always jumps to the next element, usefull if you dont know the next element (for example if you deleted/unset some of them before) or if you have associative arrays like $user['name'], $user['city'], $user['zipcode']...
     
    falcondriver, Dec 26, 2007 IP
  5. zantarm

    zantarm Banned

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I couldnt quite well understand
     
    zantarm, Dec 27, 2007 IP