I’ve been writing PHP for a long time now. And one of the things that I see quite often is that people just dont get multidimensional arrays, and how to effectively store data in them for looping. Allow me to lead by example.
- $store[‘fruit’][‘apple’]=3;
- $store[‘fruit’][‘orange’]=1;
- $store[‘berries’][‘blueberries’]=100;
- $store[‘berries’][‘strawberries’]=100;
- $store[‘fruit’][‘mango’]=5;
- foreach ( $store as $type => $val ) {
- foreach ( $val as $name => $qty ) {
- echo “We have {$qty} {$name} ({$type} are good for you)rn”;
- }
- }
of course where the array comes from and what you do with it are completely optional. But it’s important to know that the array indexes should be as informational as the array values. Otherwise you arent using arrays properly.