Jump to content


Photo

*SOLVED*making associative array keys read only, or sanity checking it's values?


  • Please log in to reply
4 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 05 September 2006 - 05:05 PM

Is there an easy way to make the keys of an associative array read-only, and preferably the data type of the value? I am passing an associative array of object vars to a client side Flash script where someone else will be writing the code to update it and pass it back. I want to make sure I can safely reassign the updated data back to the object, is there an elegant way to do this, or should I just loop through and compare the new array with the original and exit on error if anything changed?

thanks!
...drkstr

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 September 2006 - 05:08 PM

I don't think there is a way to make the keys read-only.  At least I've never come across it.

Info: PHP Manual


#3 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 05 September 2006 - 05:28 PM

Thanks for the sponce ober! I guess I'll just loop through and compare the data types of the array passed in through the parameter against those of the original. Hopefully I don't pick up/lose any keys.

thanks again!
...drkstr

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 05 September 2006 - 08:37 PM

You could put a check in the code
<?php 
$array = array ('x' => 1, 'y' => 2, 'z' => 3);

$orig_keys = array_keys($array);

    //
    // processing here
    //

$modified_array = array ('x' => 4, 'y' => 5, 'w' => 6);

$keys = array_keys($modified_array);

if ($keys == $orig_keys) {
    echo 'OK';
}
else {
    echo "Keys changed";
}

?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 06 September 2006 - 01:21 AM

That's a handy little trick. I didn't know you could compare arrays directly with the '==' operator.

I think I now have enough info to make sure nothing jiggy happens to my array in the Flash code.

Thanks everyone for the help!
...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users