简体   繁体   中英

How to pull data from complex array in php?

I am creating the post in the following form:

while ( $tracks = mysql_fetch_array($trackstring) ){
    $tracks = mysql_fetch_array($trackstring) ){
echo "<li class="tracklist">
<input type="text" name="tracknum-[$tracks['song_id']]" 
value="".$tracks['song_tracknumber']"/> 
<input type="text" name="trackname[$tracks['song_id']]" 
value="".$tracks['song_title'].""/> 
</li>";
    }

it's creating a $_POST array that looks like this:

Array ( [tracknum] => Array ( [13] => 1 [14] => 2 [15] => 3 ) [trackname] => Array ( [13] => One Beat [14] => Faraway [15] => Oh! ) 

essentially I want
1 - One Beat to go into id=13 2 - Faraway to go into id=14

I can see all the data there, but I'm not really sure how to manipulate it... I'm not so hot with arrays. How do I reference these with $_POST['????'] involving the id values

use

$_POST['trackname'][13]

to get "One Beat".

Try to use

print_r($_POST);

That will give you your POST clearly indented, so you can easily navigate through it.

And then, edit your question to sound more clear, if you still need help.

big thanks to cranial-bore at the sitepoint forums :)

$tracknum = $_POST['tracknum'];
$trackname = $_POST['trackname'];
if(count($tracknum) == count($trackname)){ // make sure both the array have same elements
    foreach($tracknum as $songid=>$tracknumber){
        $song_tracknumber = $tracknum[$songid];
        $song_title = $trackname[$songid];
        $sql = "UPDATE songs SET song_tracknumber='$song_tracknumber', song_title='$song_title' WHERE song_id=$songid";
    }
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM