Php arrrays

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I have the following data:

a,b x1
a,c x2
a,d x3

How would I store this in a way (array?) such that:

a
-b
--x1
-c
--x2
-d
--x3
b
.... etc...

Got to be simple!

Any pointers? Or alternative way?
 
Last edited:
robmiller said:
Code:
$lines = file('file.txt');
$data = array();
foreach ( (array) $lines as $line ) {
    list($a, $b, $c) = sscanf($line, '%s,%s %s');
    $data[$a][$b] = $c;
}

You're going to learn to program yourself one day, right?
Damn right I will... I'm trying to learn by example at the moment... I'm getting better! I think :S


If I could just bother you with one more thing...?

I've discovered a problem using the following code (not tidied yet):

Code:
<?
$lines = file('file.txt');
$data = array();
foreach ( (array) $lines as $line ) {
    list($a, $b, $c) = sscanf($line, '%s %s %s');
    $data[$a][$b] = $c;
	 
}

foreach ($data as $key => $value) 
{ 
    echo "<li>$key 0 ";

	foreach ($value as $key1 => $value1){
	
	echo $value1." "; 
	}
} 

?>

If "a b 153" for instance is in the file, "b a 153" will not be there - as the value would also equal 153 (thus avoiding redundancy). Is there a way to modify the above to include the value from "a b" for "b a" and so on...?

If that didn't make sense I'll try and explain a bit better....
 
Last edited:
Back
Top Bottom