Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Help adding values to an array

    I have a foreach loop (runs 3 times for jpg, gif and png images) doing a glob search for image files, and i want to store them all in an array, however at the minute the array looks like this

    Code:
    Array ( [0] => Array ( [0] => ../photos/1.jpg [1] => ../photos/10.jpg [2] => ../photos/11.jpg [3] => ../photos/12.jpg [4] => ../photos/13.jpg [5] => ../photos/14.jpg [6] => ../photos/15.jpg [7] => ../photos/16.jpg [8] => ../photos/17.jpg [9] => ../photos/18.jpg [10] => ../photos/19.jpg [11] => ../photos/2.jpg [12] => ../photos/20.jpg [13] => ../photos/21.jpg [14] => ../photos/22.jpg [15] => ../photos/23.jpg [16] => ../photos/24.jpg [17] => ../photos/25.jpg [18] => ../photos/26.jpg [19] => ../photos/3.jpg [20] => ../photos/4.jpg [21] => ../photos/5.jpg [22] => ../photos/6.jpg [23] => ../photos/7.jpg [24] => ../photos/8.jpg [25] => ../photos/9.jpg [26] => ../photos/jimmy-the-rev-sullivan.jpg [27] => ../photos/kit.jpg [28] => ../photos/mp3.jpg ) [1] => Array ( [0] => ../photos/angrynerd.png [1] => ../photos/confnerd.png [2] => ../photos/haha.png ) [2] => Array ( [0] => ../photos/Oliver17.gif ) )
    It has 3 different arrays inside it. How do i stop it doing that and make them all add to one array? Heres my code for building the array

    PHP Code:
    class createGallery {
        var 
    $result;
        function 
    getArray($dir) {
        
            
    $search = array($dir.".jpg"$dir.".png"$dir.".gif");
            foreach (
    $search as $file
            {
                
    $this->result[] = glob($file);    
            }
            echo 
    '<br /><br /><br />';
        } 

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Since glob returns an array, you're asking for it to give you back 3 arrays. Take a look at using array_merge instead.

    PHP Code:
    $this->result = array();
    $search = array($dir.".jpg"$dir.".png"$dir.".gif");
    foreach (
    $search as $file
    {
        
    $this->result array_merge($this->resultglob($file));    
    }
    echo 
    '<br /><br /><br />'
    I'm not sure if that works 100%, but its a good start.
    Last edited by Keleth; 04-22-2011 at 06:28 PM.

  • Users who have thanked Keleth for this post:

    tomharto (04-22-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    I got a fix but maybe array merge will be better so ill take a look at it . This is what i got to work
    PHP Code:
    class createGallery {
        var 
    $result;
        function 
    getArray($dir) {
        
            
    $search = array($dir.".jpg"$dir.".png"$dir.".gif");
            foreach (
    $search as $file
            {
                
    $glob glob($file);
                foreach (
    $glob as $src
                {
                    
    $this->result[] = $src;    
                }        
            }
            echo 
    '<br /><br /><br />';
        } 

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yah, that'll work (though you don't need to define $glob... you can just put glob($file) into the foreach).

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay, thanks

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tomharto View Post
    I got a fix but maybe array merge will be better so ill take a look at it . This is what i got to work
    PHP Code:
    class createGallery {
        var 
    $result;
        function 
    getArray($dir) {
        
            
    $search = array($dir.".jpg"$dir.".png"$dir.".gif");
            foreach (
    $search as $file
            {
                
    $glob glob($file);
                foreach (
    $glob as $src
                {
                    
    $this->result[] = $src;    
                }        
            }
            echo 
    '<br /><br /><br />';
        } 
    i don't think you don't need the foreach and also don't need to use $search array
    PHP Code:

    class createGallery {
        var 
    $result;
        function 
    getArray($dir) {
        
                    
    $this->result glob($dir "*.[jpg|png|gif]");
            }

    best regards

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Hmm, i tried that but it just outputs "Array"

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tomharto View Post
    Hmm, i tried that but it just outputs "Array"
    an empty array? maybe the problem is the extension spec, [jpg|..]. you are on windows?

    try this:
    PHP Code:
    $search = array($dir.".jpg"$dir.".png"$dir.".gif");
    $path implode(','$search);
    $this->result glob($path); 
    best regards

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    That brings back an empty array too =/, and yeah im using windows.

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tomharto View Post
    That brings back an empty array too =/, and yeah im using windows.
    sorry is my fault, i copy the array from a previous post
    must be:
    Code:
    $search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
    the stars are important for matching the filename.
    try this and see if work.

    best regards

  • #11
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    I tried that too before :P still nothing both times =[

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tomharto View Post
    I tried that too before :P still nothing both times =[
    this way? with implode and stars in pattern?
    PHP Code:
    $search = array($dir."*.jpg"$dir."*.png"$dir."*.gif");
    $path implode(','$search);
    $this->result glob($path); 
    best regards

  • #13
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Yeah, ill copy that code in and test it again maybe i messed it up, ill edit once tested.

    Nope, still no luck .

    I dont know if this makes annnnny difference at all, but the photos are in a parent directory (../photos to be exact), and the page im writing the class/methods on is include on an index page called the methods
    PHP Code:
    $gallery = new createGallery;
    $gallery->getArray("../photos/"); 
    . Both of those pages are in a child folder.
    Last edited by tomharto; 04-22-2011 at 09:38 PM.

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tomharto View Post
    Yeah, ill copy that code in and test it again maybe i messed it up, ill edit once tested.

    Nope, still no luck .

    I dont know if this makes annnnny difference at all, but the photos are in a parent directory (../photos to be exact), and the page im writing the class/methods on is include on an index page called the methods
    PHP Code:
    $gallery = new createGallery;
    $gallery->getArray("../photos/"); 
    . Both of those pages are in a child folder.
    relative path are tricky, try to use absolute path. Take as reference the document root. Also i'm not sure about this but i remember about some changes in expanding '..' in path in php 5.1.x, is better to avoid them.
    try this and check if the value of the path is correct, no '//' or missing '/' between directorys, and to point to the correct place:
    PHP Code:
    $dir $_SERVER['DOCUMENT_ROOT'] . "photos/";
    echo 
    $dir
    best regards

  • #15
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    That pointed to the full home/public_html path, in the index where i call the method i put

    PHP Code:
    $gallery->getArray1("http://mysite.co.uk/functionsPHP/photos/");
    $gallery->printArray(); 
    and i have this as the method
    PHP Code:
    function getArray1($dir) {
            
             
    $search = array($dir."*.jpg"$dir."*.png"$dir."*.gif");
            
    $path implode(','$search);
            
    $this->result glob($path);   
            
            echo 
    "<a href='".$dir."'>Test</a>";  
        }
    function 
    printArray() {
            
            foreach (
    $this->result as $file)
            {
                echo 
    $file."<br />";
            }
        } 
    and all the page displays is the test link, which goes to the correct folder


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •