...

View Full Version : Adding to an array works one way but not another



MrBiggZ
06-16-2010, 04:21 AM
Greetings!

I have this script



<?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! :eek: Why does it work one way and not the other??

Fou-Lu
06-16-2010, 03:36 PM
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).


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum