Advertisement

08.25.2008 at 03:00AM PDT, ID: 23674809
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

PHP 5 Multi Array - Not Creating Sub Arrays Correctly

Asked by dllfile in PHP Scripting Language, PHP for Windows, PHP and Databases

I am using PH5 and am reading a file into an array that is delimited by a "space" between each piece of information, it has 5 pieces of information, however, I only need 4 of those so I simply do not add Data1 to the array at all.

I am lost here as to what I am doing wrong, I have tried several ways of doing this and it seems to fail everytime. A couple of ways I had tried it worked fine for the first line in the file, but then did not do the same for the rest of them but only created a new main key for the Data0 but not the subdata for it.

Example file layout

Line1: Data0 Data1 Data2 Data3 Data4
Line2: Data0 Data1 Data2 Data3 Data4
Line3: Data0 Data1 Data2 Data3 Data4
Line4: Data0 Data1 Data2 Data3 Data4
Line5: Data0 Data1 Data2 Data3 Data4
Line6: Data0 Data1 Data2 Data3 Data4

etc...

All of them with exception to Data4 will not have spaces. Data4 won't always have a space, but it is possible, so using PHP5 I have told it to only allow a max of 5 sets of data once it reaches the 5th it will no longer chunk off new pieces and the Data4 will be whatever was left for the 5th piece.

Example of how I am trying to get the data to form up in the array:

Array
(
    [0] => Data0
      Array
      (
            [0] => Data4
            [1] => Data2
            [2] => Data3
      )

    [1] => Data0
      Array
      (
            [0] => Data4
            [1] => Data2
            [2] => Data3
      )
);

I was hoping to be able to call the array values or echo one out like echo $mydata[1][2];

Anyhow, if anyone can help me fix this and help me understand what im doing wrong so I can better understand arrays a bit. I have been all over some tutorials as well as the php.net site, but the one thing they are not explaining is exactly how to use them and create them, I seem to find nothing but examples of what one would look like using X type of array. I need more of the mechanics of it and less of the body.

I have attached a sample of the code exactly how I have been trying to use it though I had previously changed it around trying to form the aray different ways but failed so far.

I am using WAMP to test this locally on a Windows XP Pro install in VMWare. Not sure if that matters, but I tried to set it up as close to my hosts webserver settngs including extensions etc.. (Other than mine is XP and the host is linux) Just to be sure anything I make would most likely work on the real location it is meant to be placed.

Apache 2.2.8 and PHP 5.2.6Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
$file = 'data.dat';
$mydata = array();
$index = 0;
 
while (!feof($file)) {
	$line = fgets($file);
	// Max of 5 incase Data4 has any spaces it does not get seperated.
	$line = explode(' ', $line, 5);
	$mydata[] = $line[0];
	$mydata[$index][] = $line[4];
	$mydata[$index][] = $line[2];
	$mydata[$index][] = $line[3];
	$index++;
}
 
print_r($mydata);
[+][-]08.25.2008 at 05:20AM PDT, ID: 22304808

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, PHP for Windows, PHP and Databases
Sign Up Now!
Solution Provided By: AlexanderR
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08.25.2008 at 05:38AM PDT, ID: 22304926

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628