miguelfsf Posted April 29, 2013 Share Posted April 29, 2013 Hello, Well, basically what this code does is grab some links from a source code of a website and send them to an mp3 player. The big problem is on the get_link function, where i want to store the urls to an array. The section where im having problems is commented. Sorry for posting all this code but the functions are connected to each others. function getHost($db,$id){ if(isset($_GET['id'])){ $sql1 = "SELECT host FROM mixtape WHERE id=?"; $stm = $db->prepare($sql1); $stm->execute(array($id)); $row1 = $stm->fetch(PDO::FETCH_ASSOC); if($row1['host']=='host1'){ $sql2 = "SELECT link1 FROM faixa WHERE id_mixtape IN(SELECT id FROM mixtape WHERE id=?)"; $stm = $db->prepare($sql2); $stm->execute(array($id)); $rows_affected = $stm->rowCount(); $array=array(); if (count($rows_affected) > 0) { for($i=1; $i <= $rows_affected; $i++) { $row2 = $stm->fetch(PDO::FETCH_ASSOC); $url=$row2['link1']; get_Link($db,$url,$i,$rows_affected,$array); } } } } } function get_Link($db,$url,$pos,$rows_affect,$array){ $find = 'url:'; $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; $data = file_get_contents($url); $data = explode("\n", $data); for ($line = 0; $line < count($data); $line++) { if (strpos($data[$line], $find) !== false) { $link = preg_replace($reg_exUrl,"", $data[$line]); $v[]=$link; } } if($pos!=$rows_affect-1){ $url="mylink.com/".$link."|"; }else{ $url="mylink.com/".$link."&"; } $array[$pos]=$url; var_dump($array); // Here says that are 3 values in the array. True if($pos==$rows_affect-1){ var_dump($array); // Here is only showing the last value in the array. Why? player($db,$array); } } function player($db,$array){ if(isset($_GET['id'])){ foreach($array as $i=>$item){ echo $item; } } } Link to comment https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/ Share on other sites More sharing options...
akphidelt2007 Posted April 29, 2013 Share Posted April 29, 2013 You are sending an empty array to get_Link every time and just adding the $pos to it. So each time the array will just have one value in it. When you say you see 3 values, I don't see how that is possible with the code you have set up. I'm willing to bet what you are seeing is the array being dumped 3 times and you have mistaken those dumps for one array. Then when it gets to the last condition it's still just showing one value. Link to comment https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/#findComment-1427133 Share on other sites More sharing options...
miguelfsf Posted April 29, 2013 Author Share Posted April 29, 2013 what you are seeing is the array being dumped 3 times and you have mistaken those dumps for one array. Yes,you correct about that i didnt explain well. So, how should I send the array from getHost to get_link? Link to comment https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/#findComment-1427136 Share on other sites More sharing options...
akphidelt2007 Posted April 29, 2013 Share Posted April 29, 2013 Yes,you correct about that i didnt explain well. So, how should I send the array from getHost to get_link? You really don't need to send the array to get_link. Just return the url from get_link in to an array in get_host. for($i=1; $i <= $rows_affected; $i++) { $row2 = $stm->fetch(PDO::FETCH_ASSOC); $url=$row2['link1']; //don't need to send $array $array[] = get_Link($db,$url,$i,$rows_affected); } //then once this loop is done you can send the array to the player function player($db,$array) Just have to make sure you return the url from the get_link function Link to comment https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/#findComment-1427139 Share on other sites More sharing options...
miguelfsf Posted April 29, 2013 Author Share Posted April 29, 2013 OMG, it worked thanks alot!!! I have been for 2 days trying to figure out this! Thank you Link to comment https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/#findComment-1427141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.