View Full Version : How to push a 2-dimensional array ?
tanel96
July 2nd, 2006, 05:24 AM
I'm trying to achieve something like this:
$fruits = array (
"fruits.jpg" => array("34262","5 Jun 09:49"),
"numbers.jpg" => array("54132","6 Jun 15:42"),
"holes.jpg" => array("12901","17 Aug 14:30")
);
Here is my code so far (haven't been able to figure out, how to push the "$size" & "$date" variables into the array):
<?php
$fruits = array();
$contents = ftp_rawlist($conn_id, "/path_to_the_folder/");
foreach ($contents as $entry) {
$entry = ereg_replace( "([\x20]+)", "\x20", $entry);
$entry = explode("\x20", $entry);
$filename = $entry["8"];
$size = $entry["4"];
$date = "".$entry["6"]." ".$entry["5"]." ".$entry["7"]."";
if($filename != "." && $filename != ".."){
array_push($fruits ,$filename);
}
}
?>
icio
July 2nd, 2006, 05:03 PM
When you're wanting to set definate keys (which in this case is the filenames), you do not `push`, you do it just as you say it:
$array['key'] = $value;
If we apply this to your case, we should come up with the following
$fruits[$filename] = array($size, $date);
Overall,
<?php
$fruits = array();
$contents = ftp_rawlist($conn_id, "/path_to_the_folder/");
foreach ($contents as $entry) {
$entry = ereg_replace( "([\x20]+)", "\x20", $entry);
$entry = explode("\x20", $entry);
$filename = $entry["8"];
$size = $entry["4"];
$date = "".$entry["6"]." ".$entry["5"]." ".$entry["7"]."";
if($filename != "." && $filename != ".."){
$fruits[$filename] = array($size, $date);
}
}
?>
Hope this help :thumb:
bwh2
July 2nd, 2006, 06:27 PM
ugly codesyou plan on adding to that comment, or are you just going to leave it at that?
deletedUser2352352
July 3rd, 2006, 05:27 AM
well that doesn't help so how would you do it?
icio
July 3rd, 2006, 05:52 AM
well that doesn't help so how would you do it?You would do it like I said ^^^ Post #2
deletedUser2352352
July 3rd, 2006, 05:55 AM
You would do it like I said ^^^ Post #2
ha ha ha yeah i see but its what samotboy said ;)
tanel96
July 3rd, 2006, 07:23 AM
dammit... that just isn't doing the trick... i cant sort it this way with natsort()... i'll explain soon :bandit:
oh, and what's with the "ugly code"....
icio
July 3rd, 2006, 11:15 AM
Here's something that I overlooked. When you're looking at an array, the indexes are integers, not strings.
You need to access keys like $entry[0] as oppose to $entry["0"], I think.
I've added some comments in the code. If you uncomment them you will be presented with some info to help you debug :thumb:
<?php
$fruits = array();
$contents = ftp_rawlist($conn_id, "/path_to_the_folder/");
// // Test to see if you have the correct sort of array that you're expecting (should be a file list by the looks of things);
// echo "<pre>"; print_r($contents); echo "</pre>";
foreach ($contents as $entry) {
$entry = ereg_replace( "([\x20]+)", "\x20", $entry);
$entry = explode("\x20", $entry);
// // Test to see if you have the correct sort of array that you're expecting (should be a file list by the looks of things);
// echo "<pre>"; print_r($entry); echo "</pre>";
$filename = $entry[8];
$size = $entry[4];
$date = "".$entry[6]." ".$entry[5]." ".$entry[7]."";
if($filename != "." && $filename != ".."){
$fruits[$filename] = array($size, $date);
}
}
?>
Digitalosophy
July 3rd, 2006, 11:19 AM
you plan on adding to that comment, or are you just going to leave it at that?
he has been warned :kommie:
tanel96
July 3rd, 2006, 11:25 AM
here's how i unraveled the mystery :)
This code will output the contents of a folder in alphabetical order with the filesize and the date the files was last updated :flower:
<?php
$fruits = array();
$contents = ftp_rawlist($conn_id, "/path/to/the/folder/");
foreach ($contents as $entry) {
$entry = ereg_replace( "([\x20]+)", "\x20", $entry);
$entry = explode("\x20", $entry);
$filename = $entry["8"];
$size = $entry["4"];
$date = "".$entry["6"]." ".$entry["5"]." ".$entry["7"]."";
if($filename != "." && $filename != ".."){
$loom = $filename."|".$size."|".$date;
array_push($fruits,$loom);
}
}
natcasesort($fruits);
foreach ($fruits as $value) {
$pieces = explode("|", $value);
echo "filename: ".$pieces["0"]." size: ".$pieces["1"]." date: ".$pieces["2"]."<br>\n";
}
?>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.