Well as the title describes I need to know how to insert a multidimensional arrays into a mySQL database.. I have 5 columns in the first table (time,date,competition,first_team,second_team) and another table has 2 columns (links, home_away) here what the array outputs: Array ( [0] => Array ( [0] => time [1] => date [2] => competition [3] => first_team [4] => second_team [5] => Array ( [0] => http://blabla.com [1] => http://blabla2.com ) [6] => Array ( [0] => Home [1] => Away ) ) [1] => Array ( [0] => time [1] => date [2] => competition [3] => bla_team [4] => bla2_team [5] => Array ( [0] => http://blabla.com [1] => http://blabla2.com ) [6] => Array ( [0] => AWAY [1] => HOME ) ) ) Code (markup): I want to insert [0] => time [1] => date [2] => competition [3] => first_team [4] => second_team Code (markup): into first table then insert [5] => Array ( [0] => http://blabla.com [1] => http://blabla2.com ) [6] => Array ( [0] => Home [1] => Away ) Code (markup): into table 2 any ideas? thanx in advance
You can just serialize it http://php.net/serialize Then http://php.net/unserialize and you get back your array in the code.
That depends, because you would have to do a table in the database with time,date,competition,first_team,second_team columns It really depends on how much you know at the moment, if you just want to log a plain array you can just serialize it into the database and unserialize it in the code e.g. $array = array('one','two','three'); $array = serialize($array); var_dump($array); $unseralize = unserialize($array); var_dump($unserialize); PHP: Something tells me you might want to just put it over the database and query the server then do the loop.
i want to insert each element inside specific column... for instance time date competition first_team second_team I want to insert each element from the above into my table columns.. like competition inside competition coloumn and so on... serializing is not what I'm lookin for...I'm sure there is somthing with for loop..I found somthing but with normal arrays not multidimensional arrays... I might implement my own codes...there must b away to do this.. anyway thanx for ur help...
Yes, you will have to learn about mysql, get phpmyadmin to set up the tables and columns and a few other things. Everything you have said is do-able, you just need to research
I think that i need to use for loops.. yesterday i made this script that posts all elements inside the array.. Code: for($i = 0; $i < 10; $i++) //loop for first array elements { for($j = 0; $j < 7; $j++)//loop for second array elements { for($k = 0; $k <200; $k++)//loop for third array elements { echo $array[$i][$j][$k]; } } } but still confused how to get each element variable..do I need to define somthing?? any Ideas??
Web's pretty thin on this stuff....far as databases. Maybe this will help as how to structure the loops: http://www.webcheatsheet.com/PHP/multidimensional_arrays.php Just to save you a trip there, here's the code snippet for handling a 2-dim array. Um after I look at it, aint sure it helps or not. Your example has arrays and values right? for ($row = 0; $row < 3; $row++) { echo "<li><b>The row number $row</b>"; echo "<ul>"; foreach($shop[$row] as $key => $value) { echo "<li>".$value."</li>"; } echo "</ul>"; echo "</li>"; } PHP:
Maybe this will help more: $david = array ( richie=>array ( Sue, Natasha ), Mason=>array ( Nichole, Salma, Amber ), Rose, Floyd ); echo "<pre>"; print_r($david); echo "</pre>"; while (list($var, $val) = each($david)) { if(is_array($val)) { print "$var \n"; while (list($var2, $val2) = each($val)) { print "$val2 "; } } else { print "$val \n"; } } PHP: Outputs : richie Sue Natasha Mason Nichole Salma Amber Rose Floyd Which isn't exactly what you want but closer. Has to be a better way of doing it though.
ok I tried ur way but it was same like foreach loop..anyway I found good solution for now..check snippet: $first_element=count($match_array); for($i=0; $i<$first_element; $i++) { ///////////////////////////////date //indexing 1st element in 1st array $date=$match_array[$i][0]; ///////////////////////////////time $tmp_time=$match_array[$i][1]; //indexing 2nd element in 1st array /////////////////////////////inserting data database $query = "bla bla bla". //////////////////////////////showing data echo "<br>date: ".$date."<br>"; echo "time: ".$time."<br>"; echo "competition: ".$match_array[$i][2]."<br>"; echo "first team: ".$match_array[$i][3]."<br>"; echo "second team: ".$match_array[$i][4]."<br>"; /////////////////////////////////////////////////////////links and other data $second_element=count($match_array[$i][5]); //counting number of elements insde this array for($k=0; $k<$second_element; $k++) { $links=$match_array[$i][5][$k]; //getting all elements from this array(6th element in 1st array) $links_type=$match_array[$i][6][$k];//getting all elements from this array (7th element in 1st array) //////////////////////////////////////////////////////sql query for my links and other data in arrays 6-7 $query = "bla bla bla". ///////////////////////////echo the data and so on echo $match_array[$i][5][$k]; echo $match_array[$i][6][$k]; } } echo ‘done’; Code (markup): now everything worx like charm...as I told u its 2 or 3 for loops no need to add while loop or foreach loop...anyway thanx for ur help really appreciated
Hi I am having this same issue right now. However I do not need to echo it to the screen. In the first post you mentioned you need it to go into the database, but in you solution you just type $query = "bla bla bla"...welll I need to know what the bla bla bla is lol. my query is inserting info...but it is inserting array[name], array[relation] and so on. anyone know how to possibly fix this?