Forum Moderators: coopster

Message Too Old, No Replies

2 D associative Array

Array logic or Echo logic wrong?

         

henry0

2:15 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This an example of what I try to achieve:
(never been in love with those :) )
<<<
$products= array( array( Code=> "AAA",
Description => $aaa,
Price => $p_a
),
array( Code=> "BBB",
Description => $bbb,
Price => $p_b
),
array( Code=> "CCC",
Description => $ccc,
Price => $p_c
)
);

// to read it
for ($row=0; $row < 3; $row++ )
{
while ( list( $key, $value ) = each( $products[ $row ] ) )
{
echo "¦ $value";
}
echo"<br>";
}

>>>

coopster

4:04 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not sure what you are asking yet. The output of the above will printout something like:
¦ AAA¦ AAA Description¦ 1.23 
¦ BBB¦ BBB Description¦ 2.23
¦ CCC¦ CCC Description¦ 3.23

Is this not what you want? What is the expected output?

jatar_k

4:10 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



something is wrong with the logic there, not walking the array properly

this works

<? 
$products= array( array( Code=> "AAA", Description => $aaa, Price => $p_a),
array( Code=> "BBB", Description => $bbb, Price => $p_b),
array( Code=> "CCC", Description => $ccc, Price => $p_c));
// to read it
for ($row=0; $row < 3; $row++ ) {
foreach ($products[$row] as $key => $value) {
echo "¦ $value";
}
echo"<br>";
}
?>

jatar_k

4:10 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the original code wouldn't work for me, I switched all undeclared vars to strings obviously

<added>strangely, I tried the original code again and it worked, not sure why, must have been a bad upload or something

so I don't know what you're asking either ;)

henry0

4:25 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster and jatar_K
Thank you for your help
I feel like very much brain dead

I was still calling the old file
figured it for you mentioned a bad upload :)

Well at least it's a working example

coopster

5:59 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Does this mean the issue is resolved already, henry0? Or am I still missing something here? I'm about as confused as you are ;)

henry0

6:23 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, Yes issue is resolved
as jatar-k and you mentioned in either ways it worked
and was already working

But it echoed a blank page and no error so I looked and looked again at the script but did not find what was wrong!

Then I posted an excerpt, and you two were also kind of puzzled untill jatar_k suggested a bad upload

well, I simply was calling the old script
thank you.
At least it's a demo of 2D array using associative :)
and how to show the results

don't be mad at me!

coopster

8:09 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Here is something I use sometimes to print them out too:
$out = ''; 
foreach ($products as $array) {
$out .= implode(' ¦ ', array_map('htmlentities', $array)) . "<br>";
}
print $out;

Not so different from what you have there except we don't have to worry about counting how many entries are in the array and it auto-formats the variables for html output with htmlentities(). If you want the pipe on the front of each line as well you would just prepend that to the $out variable concatentation there.

henry0

8:33 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes an easier way
And since I might add more dynamism
It will make my life easier by not worrying about the # of rows