Jump to content

PHP Array only stores last value


Go to solution Solved by akphidelt2007,

Recommended Posts

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."&amp";
        }


        $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; 
            }
        }
    }
Edited by miguelfsf
Link to comment
https://forums.phpfreaks.com/topic/277417-php-array-only-stores-last-value/
Share on other sites

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.

Edited by akphidelt2007
  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.