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.
Results 1 to 2 of 2
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    276
    Thanks
    39
    Thanked 0 Times in 0 Posts

    Question Adding to an array works one way but not another

    Greetings!

    I have this script

    PHP Code:
    <?php

    $f1 
    file('file-one.txt');
    $f2 file('file-two.txt');

    //$f1 = file('sidemenu_top.txt');
    //$f2 = file('sidemenu_bottom.txt');


    foreach ($f1 as $f1Lines => $f1) {
            
    $nf[$f1Lines] = $f1;
            }

        
    for (
    $midFile=($f1Lines+1);$midFile<=($f1Lines+1)+10;$midFile++) {
            
    $nf[$midFile] = $midFile;
            }


    foreach (
    $f2 as $f2Lines => $f2) {
        
    $idx $f2Lines $midFile;
        
    $nf[$idx] = $f2;
        }

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

    ?>
    My first attempt was with file-one and file-two which are like this:
    file-one.txt
    apples
    oranges
    grapes
    strawberries
    file-two.txt
    dog
    cat
    mouse
    zebra
    and I get the results:
    Array
    (
    [0] => apples

    [1] => oranges

    [2] => grapes

    [3] => strawberries
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [11] => 11
    [12] => 12
    [13] => 13
    [14] => 14
    [15] => dog

    [16] => cat

    [17] => mouse

    [18] => zebra
    )
    But when I use the other two files:
    sidemenu_top.txt
    <!--

    /*
    Configure menu styles below
    NOTE: To edit the link colors, go to the STYLE tags and edit the ssm2Items colors
    */
    YOffset=150; // no quotes!!
    XOffset=0;
    staticYOffset=30; // no quotes!!
    slideSpeed=20 // no quotes!!
    waitTime=100; // no quotes!! this sets the time the menu stays out for after the mouse goes off it.
    menuBGColor="black";
    menuIsStatic="yes"; //this sets whether menu should stay static on the screen
    menuWidth=150; // Must be a multiple of 10! no quotes!!
    menuCols=2;
    hdrFontFamily="verdana";
    hdrFontSize="2";
    hdrFontColor="white";
    hdrBGColor="#170088";
    hdrAlign="left";
    hdrVAlign="center";
    hdrHeight="15";
    linkFontFamily="Verdana";
    linkFontSize="2";
    linkBGColor="white";
    linkOverBGColor="#FFFF99";
    linkTarget="_top";
    linkAlign="Left";
    barBGColor="#444444";
    barFontFamily="Verdana";
    barFontSize="2";
    barFontColor="white";
    barVAlign="center";
    barWidth=20; // no quotes!!
    barText="SIDE MENU"; // <IMG> tag supported. Put exact html for an image to show.

    ///////////////////////////

    // ssmItems[...]=[name, link, target, colspan, endrow?] - leave 'link' and 'target' blank to make a header
    And sidemenu_bottom.txt (there is a blank line at the beginning of that file)

    buildMenu();

    //-->
    I get this for a result:
    Array
    (
    [0] =>
    )
    I'm uber perplexed! Why does it work one way and not the other??

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Looks to me like you're simply merging two arrays together.
    Depending on how you view this depends on what the problem is. If you are viewing this as a result in your browser, you will not get any output. The first line (offset 0) in your first file is '<!--' which of course now makes everything to follow a comment. Always check the source.
    There is no reason for this type of work to put together two arrays (if you need them as arrays).
    PHP Code:
    $f1 file('file1.txt');
    $f2 file('file2.txt');

    $aFileArray array_merge($f1$f2); 
    Or, if you need this to be a string, you can use file_get_contents, or fpassthru to sweep up the entire file into a string and append it to a variable.
    Last edited by Fou-Lu; 06-16-2010 at 03:41 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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