NYCPHP Meetup

NYPHP.org

[nycphp-talk] Creating multidimensional arrays

DeWitt, Michael mjdewitt at alexcommgrp.com
Wed May 14 09:47:02 EDT 2003


Emmanuel,

Like yourself, I have been a big fan of multidimensional arrays (MDA) in
PHP. One of my favorite uses is in using an MDA to hold product information
or perhaps a special price which goes into effect if the correct "code" used
in "unlocking" the array.  For example,

consider an array like this

promo[product_code][promo_code][domestic_price][foreign_price][source_code]

and in PHP you build it like this

$promo['abc']['123']=array('pdprice'=>167,'pfprice'=>197,'src_cde'=>'abc123'
);

You can see that the promo_code, '123' works as a key to unlocking the
special pricing and source code that should be used on the order.  Any value
for the promo_cde other than '123' and the value of pdprice will be null.

Yes, you could do this via a database, but we didn't feel the effort in
building the additional needed table maintenace modules worth it when merely
adding and deleting entries to the $promo array was so easy.

I haven't noticed this being slow, but in one of the NYPHP monthly meetings
I remember that MDAs were to be avoided for performance reasons.  I think it
was Dan Cowgill of Community Connect, but I may be mistaken.  Does anyone
else remember this being discussed?

Mike

  

> -----Original Message-----
> From:	Emmanuel. M. Decarie [SMTP:emm at scriptdigital.com]
> Sent:	Wednesday, May 14, 2003 8:59 AM
> To:	NYPHP Talk
> Subject:	[nycphp-talk] Creating multidimensional arrays
> 
> Hello,
> 
> I have this little function that work well for me, but I was 
> wondering if it could be optimized. I use it to set the default value 
> of checked radio buttons. Also, while doing this function, I 
> discovered that PHP can autoinstantiate associative arrays in depth. 
> For ex.
> 
> $ar = array ();
> $ar['one']['two']['three'] = 'hello world';
> 
> echo $ar['one']['two']['three'];
> 
> --> hello world
> 
> I must say that PHP's array are one of the nicest feature of the language.
> 
> --------------------------------------------------------
> Here's what it look inserted in the HTML page:
> --------------------------------------------------------
> 
> <hr>
> <form>
> Are you ugly?
> Yes
> <input type="radio" name="choice_1" value="1" <?php echo 
> $radioGroup['1']['1'] ?>>
> No
> <input type="radio" name="choice_1" value="1" <?php echo 
> $radioGroup['1']['2'] ?>>
> <hr>
> Are you tall?
> Yes
> <input type="radio" name="choice_2" value="1" <?php echo 
> $radioGroup['2']['1'] ?>>
> No
> <input type="radio" name="choice_2" value="1" <?php echo 
> $radioGroup['2']['2'] ?>>
> <hr>
> Are you funny?
> Yes
> <input type="radio" name="choice_3" value="1" <?php echo 
> $radioGroup['3']['1'] ?>>
> No
> <input type="radio" name="choice_3" value="1" <?php echo 
> $radioGroup['3']['2'] ?>>
> </form>
> 
> --------------------------------------------------------
> And here's the function:
> --------------------------------------------------------
> 
> function assignValues ($maxOutArrays, $maxInArrays, $arSelection) {
> 
>    $counter1   = 1;
>    while ( $counter1 <= $maxOutArrays ) {
> 
>      $counter2 = 1;
>      while ( $counter2 <= $maxInArrays ) {
>        if ( !isset ($arSelection[$counter1][$counter2]) ) {
>          $arSelection[$counter1][$counter2] = '';
>        }
>        $counter2++;
>      }
>      $counter1++;
>    }
>    return $arSelection;
> }
> 
> $radioGroupDefaultValues['1']['2'] = 'checked';
> $radioGroupDefaultValues['2']['1'] = 'checked';
> $radioGroupDefaultValues['3']['1'] = 'checked';
> 
> $radioGroup = assignValues (3, 2, $radioGroupDefaultValues);
> 
> --------------------------------------------------------
> And the output show with var_dump ($radioGroup)
> --------------------------------------------------------
> 
> array(3) {
>    [1]=>
>    array(2) {
>      [2]=>
>      string(7) "checked"
>      [1]=>
>      string(0) ""
>    }
>    [2]=>
>    array(2) {
>      [1]=>
>      string(7) "checked"
>      [2]=>
>      string(0) ""
>    }
>    [3]=>
>    array(2) {
>      [1]=>
>      string(7) "checked"
>      [2]=>
>      string(0) ""
>    }
> }
> 
> Now I have a big form with 50 radio buttons in 10 groups of 5 radio 
> buttons. I didn't benchmark this function, it seems that it run well, 
> but if there is a better way to do it, I'd like to hear it.
> 
> TIA
> 
> -- 
> ______________________________________________________________________
> Emmanuel Décarie / Programmation pour le Web - Programming for the Web
> Radio UserLand/Frontier - Perl - PHP - Javascript
> <http://scriptdigital.com/>
> Blog: <http://blog.scriptdigital.com> - AIM: scriptdigital
> 
> 
> 
> --- Unsubscribe at http://nyphp.org/list/ ---
> 



More information about the talk mailing list