11Tami Posted August 17, 2007 Share Posted August 17, 2007 Hello, how would I convert an array like this: $thesefiles = array('something1.txt','another2.txt','3.txt'); Into an array where numbers are put into the array instead. I tried this but it doesn't work. $thesefiles = new Array(); $thesefiles [1] = ["something1.txt"]; $thesefiles [2] = ["another2.txt"]; $thesefiles [3] = ["3.txt"]; Please let me know how to number the array in php. Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/ Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 first off $thesefiles = array('something1.txt','another2.txt','3.txt'); will put numbers in as for the example you did, your almost right $thesefiles = Array(); $thesefiles[1] = "something1.txt"; $thesefiles[2] = "another2.txt"; $thesefiles[3] = "3.txt"; useful command is echo "<pre>"; print_r($thesefiles); Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-327214 Share on other sites More sharing options...
11Tami Posted August 18, 2007 Author Share Posted August 18, 2007 Thank you, the code isn't reading the array number. Here's the rest. Its supposed to go from one item in the array to the next on each visit. Anyone know why this code isn't reading the array numbers to select the next item? Thanks a lot. <?php $maximum = 2; $a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; setcookie("e", $a, time()+60*60*24*180); $currentFile = $a; $allfiles = Array(); $allfiles[1] = "thisfile.txt"; $allfiles[2] = "thatfile.html"; foreach($allfiles as $file): if(preg_match("/".$currentFile."\.txt/","/".$file."\.txt/")): $file_content = file_get_contents($file); break; elseif(preg_match("/".$currentFile."\.html/","/".$file."\.html/")): $file_content = file_get_contents($file); break; endif; endforeach; echo $file_content; exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-327233 Share on other sites More sharing options...
MadTechie Posted August 18, 2007 Share Posted August 18, 2007 if your trying to pull the file from the allfiles array try this $maximum = 2; $a = (isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum)? intval($_COOKIE['e'])+1 : 1; setcookie("e", $a, time()+60*60*24*180); $currentFile = $a; $allfiles = Array(); $allfiles[1] = "thisfile.txt"; $allfiles[2] = "thatfile.html"; $file_content = file_get_contents($allfiles[$currentFile]); Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-327399 Share on other sites More sharing options...
d.shankar Posted August 18, 2007 Share Posted August 18, 2007 I hope this could help http://www.php.net/manual/en/function.array-merge.php Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-327461 Share on other sites More sharing options...
11Tami Posted August 22, 2007 Author Share Posted August 22, 2007 That simplifies it alot Madtechie, any way I can add a .gif to it? My other one I could add .gifs to but the array wasn't numbered would like to keep the array numbered. I looked at yours inside and out and can't figure out how. Pretty sure I don't need to merge two arrays, I just need to break off a .gif from the rest somehow. Thank very much, I hope your still out there, your helping a lot. Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-330579 Share on other sites More sharing options...
Hypnos Posted August 22, 2007 Share Posted August 22, 2007 Looking over the posted code, you should be able to change $maximum to 3, and then just add a 3rd value to the array. Untested changes to what MadTechie posted: <?php $maximum = 3; if(isset($_COOKIE['e']) && intval($_COOKIE['e']) < $maximum) { $a = intval($_COOKIE['e'])+1; } else{ $a = 1; } setcookie("e", $a, time()+60*60*24*180); $currentFile = $a; $allfiles = array( 1 => "thisfile.txt", 2 => "thatfile.html", 3 => "thatfile.gif" ); $file_content = file_get_contents($allfiles[$currentFile]); Yes, I had to change the one line if/else in to a real if/else. I'm alergic to them. Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-330585 Share on other sites More sharing options...
11Tami Posted August 22, 2007 Author Share Posted August 22, 2007 Thanks a lot, I sort of tried that but it wants the .gif in an image format, and I don't know where to put it. "<img src=\'with some correct variable in here so it gets added on to the bottom with the other echo with the rest'>"; Also I don't think I can use file_get_contents with a .gif because I believe thats just for web page files unless I'm mistaken. Anyone know how to add the .gif? Thanks very much. Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-330611 Share on other sites More sharing options...
MadTechie Posted August 22, 2007 Share Posted August 22, 2007 I assume your getting data to display on the page.. if so.. this should do it replace $file_content = file_get_contents($allfiles[$currentFile]); with <?php $getfile = $allfiles[$currentFile]; preg_match_all('/\.(.{0,3})/i', $getfile, $result, PREG_PATTERN_ORDER); $result = $result[1]; $result = $result[count($result)-1]; $fileext = strtolower($result); $file_content = ""; switch($fileext) { default: case "html": case "htm": case "txt": $file_content = file_get_contents($getfile); break; case "jpg": case "jpeg": case "gif": case "png": $file_content = "<img src=\"$getfile \" />"; break; } //echo $file_content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65532-solved-array-setup-help/#findComment-330749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.