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 MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts

    Need help with array_unshift used w/ two dim array

    Hi!

    I'm just having a hard time wrapping my mind around this one. I understand what the array_unshift function does. And it's exactly what I need!

    My script reads a directory and a process it and pick pieces from it and then wish to shuffle the order of the files with on particular file at the top of the array.

    Here's the script:
    PHP Code:
    <?php

    function expireContent($dir){
        
    $ffs scandir($dir);
        
    $c 0;

        foreach(
    $ffs as $ff){
            if(
    $ff != '.' && $ff != '..'){
                if (
    is_file($dir.'/'.$ff)) {
                    
    $finfo pathinfo($dir.'/'.$ff);
                    if (
    $finfo['filename'] != "Welcome_to_FTSC_Website-999999") {
                        echo 
    "<pre>\n";
                        echo 
    print_r($finfo);
                        echo 
    "</pre>\n";
                        
    $slides[$c]['filename'] = $finfo['basename'];
                        
    $slides[$c]['date']= substr $finfo['filename'] , strrpos($finfo['filename'], "-") +1strlen($finfo['filename'])) ;
                    }
                }
                
    $c++;
            }
        }
        
        
    shuffle($slides);
        
        
    array_unshift($slides,"Welcome_to_FTSC_Website-999999.jpg","999999");

                    echo 
    "<pre>\n";
                    echo 
    print_r($slides);
                    echo 
    "</pre>\n";    
        
    }

    expireContent($_SERVER['DOCUMENT_ROOT'] . "/images/slide-info-bg");

    ?>
    Here's the output:
    Array
    (
    [dirname] => D:/www/ftsc-v2.0/images/slide-info-bg
    [basename] => Consession_Stand-999999.jpg
    [extension] => jpg
    [filename] => Consession_Stand-999999
    )
    1
    Array
    (
    [dirname] => D:/www/ftsc-v2.0/images/slide-info-bg
    [basename] => team-photos-041713.png
    [extension] => png
    [filename] => team-photos-041713
    )
    1
    Array
    (
    [dirname] => D:/www/ftsc-v2.0/images/slide-info-bg
    [basename] => us_soccer_license_courses-042913.jpg
    [extension] => jpg
    [filename] => us_soccer_license_courses-042913
    )
    1
    Array
    (
    [0] => Welcome_to_FTSC_Website-999999
    [1] => 999999
    [2] => Array
    (
    [filename] => us_soccer_license_courses-042913.jpg
    [date] => 042913
    )

    [3] => Array
    (
    [filename] => Consession_Stand-999999.jpg
    [date] => 999999
    )

    [4] => Array
    (
    [filename] => team-photos-041713.png
    [date] => 041713
    )

    )
    I'm good until the point where I use the array_unshift fuction then I loose my two dimensionallity.

    I gave this a shot at one point :

    PHP Code:
        array_unshift($slides,'filename' => "Welcome_to_FTSC_Website-999999.jpg",'date' => "999999"); 
    But it laughed at me with an error talking about a double arrow.

    Any and all help is appreciated! =)
    Last edited by MrBiggZ; 03-30-2013 at 01:43 PM. Reason: Edit
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    try this aproach

    PHP Code:
    $files glob('/var/www/*');
    $firsFileToOutput 'index.php';

    foreach(
    $files as $value){
        if(
    is_file($value)){
            if(
    basename($value) == $firsFileToOutput){
                
    ///stuff to do with first file
                
    echo  basename($value) . '<br />';
            }
        }
    }

    //randomize
    shuffle($files);

    foreach(
    $files as $value){
        if(
    is_file($value)){
            if(
    basename($value) != $firsFileToOutput){
                
    ///stuff to do with every other file
                
    echo  basename($value) . '<br />';
            }
        }


  • #3
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    That wouldn't work because what I want to be the first file in the new array is also in the directory that I read at the beginning.

    I scan the directory, put info from the directory it into an array without that one file, shuffle the array and try to add that one item to the top of the array.

    I always want that one item to be at the zero (0) item i the array.

    Unless I'm reading it wrong .. it's still early in the morning for me! |=

    Thanks for your idea and I will look again .. maybe it'll make more sense one the mind has fully woke! (haven't written it off yet)

    COFFEE!
    Last edited by MrBiggZ; 03-30-2013 at 02:02 PM.
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    put it in simple words so i could understand.
    you want to list files in directory, and shuffle them, but keep one predefined file at 0?

  • Users who have thanked patryk for this post:

    MrBiggZ (03-30-2013)

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You were close with your last line, but you need to have the item you're unshifting inside of an array:
    PHP Code:
    array_unshift$slides, array( 
        
    'filename' => 'Welcome_to_FTSC_Website-999999.jpg'
        
    ,'date' => '999999' 
        
    )
    ); 

  • #6
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    put it in simple words so i could understand.
    you want to list files in directory, and shuffle them, but keep one predefined file at 0?
    I've had my first infusion of caffeine! Went back and looked .. and looked at the doc for the glob function. Which I've never used!

    I'll have to remember that for the future!!!


    Quote Originally Posted by Inigoesdr View Post
    You were close with your last line, but you need to have the item you're unshifting inside of an array:
    PHP Code:
    array_unshift$slides, array( 
        
    'filename' => 'Welcome_to_FTSC_Website-999999.jpg'
        
    ,'date' => '999999' 
        
    )
    ); 
    Ah .. I get it now. I would have had to make that one item I wanted to shift an array. So it's like you're sticking an array on top of an array and not just adding on item to it.

    I see now!
    Last edited by MrBiggZ; 03-30-2013 at 02:18 PM. Reason: edit
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #7
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    update: this one is wrong. do not use. check posts below

    haha
    try this: version commented for before-coffee-and-ciggie
    PHP Code:
    $files glob('/var/www/*'); //digestory to list
    $firsFileToOutput 'index.php'//file to be put on top

    $output = array();

    //this loop will capture only $firsFileToOutput
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) == $firsFileToOutput){
                
    $output[]['basename'] = basename($value);
                
    $output[]['dirname'] = dirname($value);
                
    $output[]['otherStuff'] = 'blah blah blah';
            }
        }
    }

    //randomize
    shuffle($files);

    //this one will add everything else
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) != $firsFileToOutput){
                
    $output[]['basename'] = basename($value);
                
    $output[]['dirname'] = dirname($value);
                
    $output[]['otherStuff'] = 'blah blah blah';
            }
        }
    }

    ///print_r() for debugging
    print_r($output); 
    Last edited by patryk; 03-30-2013 at 02:29 PM.

  • #8
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    partyk

    Was just writing something to that fact in the code.

    Oh, I gave up on the ciggie's many years ago! The cost was killing me more then the health aspects!

    Basically my view of array_shift was skewed. This is most def one to remember! Believe it or not .. i keep all these little test scripts and when I need to reference back to them .. they're there!
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #9
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    sorry man.... looks like i need more stimulants myself

    PHP Code:
    $files glob('/var/www/*');
    $firsFileToOutput 'index.php';
    $output = array();

    //this loop will capture only $firsFileToOutput
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) == $firsFileToOutput){
                
    $output[] = array('basename' => basename($value),
                            
    'dirname' => dirname($value),
                            
    'otherStuff' => 'blah blah blah'
                            
    );
            }
        }
    }

    //randomize
    shuffle($files);

    //this one will add everything else
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) != $firsFileToOutput){
                
    $output[] = array('basename' => basename($value),
                            
    'dirname' => dirname($value),
                            
    'otherStuff' => 'blah blah blah'
                            
    );
            }
        }

    this one is fixed

  • Users who have thanked patryk for this post:

    MrBiggZ (03-30-2013)

  • #10
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I like 'other-stuff' compared to 'foo' !!

    Basically the whole premise of the this little script is it can do the maintenance for me. The numbers on the end of the file names are dates. Once it hits that date greater than or equal to the content doesn't get shown and the file gets deleted from the server.

    Don't know why I didn't think about his months ago! >8|=|
    Last edited by MrBiggZ; 03-30-2013 at 03:09 PM. Reason: edit
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #11
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    you mean like in removing all files older than say 10 days and then displaying what's left?

  • #12
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    you mean like in removing all files older than say 10 days and then displaying what's left?
    Yep, but I can't go by what the file dates are. I donate my time to a non-profit soccer club and have slides that advertise classes, training and other activities. I stick the expire date at the end of the file name. So it the activity runs from april 1st to the 6th I make the content go away on the 7th.

    I wish my PHP skills were more than they are. Never went to school for this stuff, just sat down and started at it. Although I already have a programming background in COBOL which was never used, another sad story.

    Sometimes my efforts are in the right direction but with wrong set of tools. Such as in this case.
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #13
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    ha!
    for something like that you need only one loop.
    Code:
    foreach(...){
        if(file_is_too_old){
            //unlink file
        }else{
            //add file to output array
        }
    }
    what date format are you using? if it's unix time format, then it's all you need

  • #14
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    269
    Thanks
    39
    Thanked 0 Times in 0 Posts
    This is my final solution ..

    PHP Code:
    $files glob($_SERVER['DOCUMENT_ROOT'] . "/images/slide-info-bg/*");
    $firsFileToOutput 'Welcome_to_FTSC_Website-999999.jpg';
    $output = array();

    //this loop will capture only $firsFileToOutput
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) == $firsFileToOutput){
                
    preg_match"/[0-9]*/"substr basename($value) , strrpos(basename($value), "-") +1strlen(basename($value))) , $match);
                
    $output[] = array('basename' => basename($value),
                            
    'dirname' => dirname($value),
                            
    'date' => $match[0]
                            );
            }
        }
    }

    //randomize
    shuffle($files);

    //this one will add everything else
    foreach($files as $value){
        if(
    is_file($value)){
            if(
    basename($value) != $firsFileToOutput){
                
    preg_match"/[0-9]*/"substr basename($value) , strrpos(basename($value), "-") +1strlen(basename($value))) , $match);
                
    $output[] = array('basename' => basename($value),
                            
    'dirname' => dirname($value),
                            
    'date' => $match[0]
                            );
            }
        }
    }  

                    
    foreach (
    $output as $image) {
        
    $m substr($image['date'],0,2);
        
    $d substr($image['date'],2,2);
        
    $y substr($image['date'],4,2);

        if((
    time() > mktime (0,0,0,$m,$d,$y)) and ($image['date'] != "999999")) {
            
    unlink($_SERVER['DOCUMENT_ROOT'] . "/images/slide-info-bg/" $image['basename']);
        } else {
            echo 
    "<li data-animate='fadeIn, fadeOut'><img src='images/slide-info-bg/" $image['basename'] . "' alt='" str_replace("_"," ",substr($image['basename'],0,strpos($image['basename'],"-"))) . "' width='600px' height='350px' /></li>\n";
        }

    It works! Yeah!!!
    Last edited by MrBiggZ; 03-31-2013 at 06:26 AM. Reason: edit
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #15
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    i do have a question tho.
    if 'Welcome_to_FTSC_Website-999999.jpg' always stays the same, then why search for it?

    instead of first foreach loop you could just set your array
    PHP Code:
    $output[] = array('basename' => 'Welcome_to_FTSC_Website-999999.jpg',
                            
    // 'dirname' => $_SERVER['DOCUMENT_ROOT'] . '/images/slide-info-bg',
                            
    'date' => 999999
                             
    ); 
    also if you are not using dirname in the rest of your code then you can just comment it out. you don't need it in array anyways
    the less code you have, the easier it is to find bugs afterwards


  •  
    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
    •