Forum Moderators: coopster

Message Too Old, No Replies

Transform an array

Turn rows into columns

         

aspdaddy

9:32 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anyone got any ideas how to solve this problem posted in .NET forum.

Thanks

[webmasterworld.com...]

jatar_k

10:38 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



coding the php of it wouldn't be difficult, a loop or two

I would assume if you posted it in .NET then that answer wouldn't help though.

whoisgregg

10:47 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's just a matter of knowing what the possible options are for any given product. Then loop through the possibilities and output each, alternating how you output them based on whether or not that sale actually chose that option.

So, in untested psuedocode:

$array_of_possibilities = ('red', 'blue', 'one', 'two');
$array_of_choices = ('blue','one');
foreach($array_of_possibilities as $possible_choice){
    if(array_search($possible_choice, $array_of_choices)!==FALSE){
        echo 'Yes - '.$possible_choice;
    } else {
        echo 'No - '.$possible_choice;
    }
}

Added: I hope this is helpful -- I know pretty much nothing about ASP or .NET.

jatar_k

10:59 pm on Aug 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this code works, but again it's php. I left in my test code as well (outputting of arrays and initial array creation).

<? 
$mystuff = array();
$mystuff[] = array(001,01);
$mystuff[] = array(001,02);
$mystuff[] = array(001,03);
$mystuff[] = array(002,02);
$mystuff[] = array(003,01);
$mystuff[] = array(003,03);
echo '<pre><b>stuff:</b><br>';
print_r($mystuff);
echo '</pre>';
$mynewstuff = array();
$counter = 0;
while (isset($mystuff[$counter][0])) {
$mynewstuff["{$mystuff[$counter][0]}"]["{$mystuff[$counter][1]}"] = 'yes';
$counter++;
}
echo '<p><pre><b>newstuff:</b><br>';
print_r($mynewstuff);
echo '</pre>';
?>

this just rebuilds the array for easy output. It does rely on the existence of sequential ids but you could get around it by using a for each on output.