Page 1 of 1
Multidimensional Array from 3 different text files
Posted: Mon Nov 23, 2009 4:29 am
by rumblefish1976
I really hope someone can help me with this. I have a .txt file containing the names of hotel function rooms. Each function room has its own .txt file with all of its details.
How would I dynamically create a multidimensional array, or is this even the right way to go about it ???????
Re: Multidimensional Array from 2 different text files
Posted: Mon Nov 23, 2009 8:41 am
by AbraCadaver
Not sure of the contents of your files, but this might get you started:
Code: Select all
// get the function rooms into an array
$rooms = file('function_rooms.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// loop through rooms and then read the room file into an array
foreach($rooms as $room) {
$result[$room] = file($room . '.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
-Shawn
Re: Multidimensional Array from 3 different text files
Posted: Tue Nov 24, 2009 2:03 pm
by rumblefish1976
thanks that really helped. sadly I thought if someone showed me the starting point I could figure out the rest myself.
So in reality I have a bigger problem. I explain properlly.
1) I have a text file called 'hotels.txt'. At present it has 4 hotels (eg thePlaza), but more can be added or taken away.
2) Each hotel has its own file containing various details (thePlaza.txt). Now the 1st seven details are location, email etc. but the remaining details are names of function rooms(eg whiteRoom). Some have 2-3, one has 4 rooms.
3) Each function room has its own text file of details (thePlaza_whiteRoom.txt)
I tried using the code from the previous post to build a multidimensional array but I am going wrong somewhere. Is array_slice() approiate to use?Here's what I've got........
Code: Select all
$hotels = array();
$hotels = file('files/hotels.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($hotels as $hotel)
{
$details[$hotel] = file('files/'.$hotel.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$detail[$hotel]=array_slice($details[$hotel],7);
foreach($detail[$hotel] as $room)
{
$roomDetails[$room]=file('files/'.$hotel.'_'.$room.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
}
Any help would be greatly received !!!
Re: Multidimensional Array from 3 different text files
Posted: Tue Nov 24, 2009 3:07 pm
by AbraCadaver
Depends upon how you want to use your array but I would probably do it like this (not tested):
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', 1);
$hotels = array();
$hotels = file('files/hotels.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($hotels as $hotel) {
// I think you need 8 to get the 8th one
$details[$hotel] = array_slice( file('files/'.$hotel.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES), 8);
foreach($details[$hotel] as $room) {
// Just keep the rooms in the same array
$details[$hotel][$room] = file('files/'.$hotel.'_'.$room.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
}
print_r($details);
Then the array should look something like this:
Code: Select all
Array
(
[thePlaza] => Array
(
[whiteRoom] => Array
(
[0] => detail1
[1] => detail2
)
[redRoom] => Array
(
[0] => detail1
[1] => detail2
)
)
[theSomething] => Array
(
[someRoom] => Array
(
[0] => detail1
[1] => detail2
)
)
)
-Shawn
Re: Multidimensional Array from 3 different text files
Posted: Wed Nov 25, 2009 2:44 am
by rumblefish1976
Brilliant. and Argh I was so close. I was trying to do it like this : $details[$hotel[$room]] (spot the newbie !!) instead of $details[$hotel][$room].
But it works better keeping the $roomDetalils as it organises the arrays the way I wanted.
Code: Select all
$hotels = array();
$hotels = file('files/hotels.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($hotels as $hotel)
{
$details[$hotel] = array_slice( file('files/'.$hotel.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES), 7);
foreach($details[$hotel] as $room)
{
$roomDetails[$hotel][$room] = file('files/'.$hotel.'_'.$room.'.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
<div class="image_holder">
<a href="#">
<img src="images/<?php echo $room ?>.jpg" onmouseover="makeBright(this)" onmouseout="makeOpaque(this)"/>
</a>
<div class="desc_div">
The <?php print_r($roomDetails[$hotel][$room][0]) ?> Room.
<br/> Maximum Capacity : <?php print_r($roomDetails[$hotel][$room][2]) ?>
</div>
</div>
<?php
}
}
?>
</div>
<?php print_r($roomDetails); ?>
Again, thank-you thank-you thank-you !!!!!!
Here's it up and running if you wanna see
http://dunluce.infc.ulst.ac.uk/ac08tb/c ... 4/test.php
Re: Multidimensional Array from 3 different text files
Posted: Wed Nov 25, 2009 6:14 am
by rumblefish1976
Arhg!!!! Apparently I have to store the details of each hotel that were sliced.
Code: Select all
Array (
[thePlaza] => Array (
[0] => detail1
[1] => detail2
[2] => detail1
[3] => detail2
[4] => detail1
[5] => detail2
[whiteRoom] => Array (
[0] => detail1
[1] => detail2
)
)