drkstr Posted September 5, 2006 Share Posted September 5, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19786-solvedmaking-associative-array-keys-read-only-or-sanity-checking-its-values/ Share on other sites More sharing options...
ober Posted September 5, 2006 Share Posted September 5, 2006 I don't think there is a way to make the keys read-only. At least I've never come across it. Link to comment https://forums.phpfreaks.com/topic/19786-solvedmaking-associative-array-keys-read-only-or-sanity-checking-its-values/#findComment-86509 Share on other sites More sharing options...
drkstr Posted September 5, 2006 Author Share Posted September 5, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19786-solvedmaking-associative-array-keys-read-only-or-sanity-checking-its-values/#findComment-86526 Share on other sites More sharing options...
Barand Posted September 5, 2006 Share Posted September 5, 2006 You could put a check in the code[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";}?> [/code] Link to comment https://forums.phpfreaks.com/topic/19786-solvedmaking-associative-array-keys-read-only-or-sanity-checking-its-values/#findComment-86654 Share on other sites More sharing options...
drkstr Posted September 6, 2006 Author Share Posted September 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19786-solvedmaking-associative-array-keys-read-only-or-sanity-checking-its-values/#findComment-86830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.