Languages :: PHP :: PHP and data structures |
|||
| By: progGoon |
Date: 22/03/2003 00:00:00 |
Points: 500 | Status: Answered Quality : Excellent |
|
I am setting out to learn PHP, I come from a heavy C background and I would like to use some simple data structures, but I do not have the slightest clue how to set this up in PHP here is what I am talking about(in C of course) struct foo { char *name; char *output; } // disregard the non-allocation of strings, just example foo fooArray = {{"one", "first"}, {"two", "second"}}; yadda yadda yadda then how do I access these in the array if(strstr(fooArray[0].name, CONTANT_ONE) == 0) { } I need to setup some data structures similar to that above and I am just stuck at where to start. Do I have to use the array functions to put this into the structure and also to access the members? Sorry if this is a stupid question, I am new to this whole "compile/interpret" on the fly scripting language. Just seems odd. |
|||
| By: TheFalklands | Date: 23/03/2003 07:09:00 | Type : Comment |
|
| Hi, Welcome to PHP coding. Now you need no struct etc. here. You can use arrays to do what you intend to do :) like this : $foo_array=array("key1"=>"value1","key2"=>"value2"); so you can access it as $foo_array["key1"] Please see the PHP manual for using arrays in diff. ways. Enjoy PHP! Cheers, |
|||
| By: sumotimor | Date: 23/03/2003 07:15:00 | Type : Comment |
|
| TheFalklands, So if I want a "contsant" array, can I put that in my constants.php file and just include it and I will be able to access it? Wow....quite a bit easier and quicker without having to worry about all the memory management in C. Thanks, sumotimor |
|||
| By: VGR | Date: 23/03/2003 07:19:00 | Type : Answer |
|
| structs are records PHP doesn't have records. This has to be implemented as an Object IMHO) <A HREF="http://www.php.net/manual/en/language.types.php">http://www.php.net/manual/en/language.types.php</a> and arrays are a basic structure. You don't have to allocate nor dispose the variables, they are garbage-collected at the end of execution of the script (except for explicitly-set SESSION data) your foo array would be declared, allocated and filled-in all in one statement (like in BASIC) by : $fooArray=array(0=>array(0=>'one',1=>'two'),1=>array(0=>'two',1=>'second')); (it's good common practice to first declare&empty the variable as an array, though : $fooArray=array(); ) This is because arrays are associative in PHP ($key => $value) pairs. Also, all variables are POLYMORHPIC. Usually (except for floating-point or type-forced operations), you never have to typecast. Personally, I would declare $fooArray differently for the same achieved result : $fooArray=array(); $fooArray[0]=array(0=>'one',1=>'two'); $fooArray[1]=array(0=>'two',1=>'second'); As for strstr(), this function exists in PHP and mimicks the C one, so you shouldn't have any problem. Constants do exist also, define()-ed Arrays are open arrays (no limit nor size specified). This will show you the power of PHP arrays/vectors : (<A HREF="http://www.php.net/manual/en/language.types.array.php">http://www.php.net/manual/en/language.types.array.php</a>) <?php $arr = array(5 => 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?> <?php // this $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); // is completely equivalent with $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4; // key will be 0 $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; // will result in the array array(0 => 'a' , 1 => 'b' , 2 => 'c'), // or simply array('a', 'b', 'c') ?> <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo "Do you like $color?\n"; } /* output: Do you like red? Do you like blue? Do you like green? Do you like yellow? */ ?> <?php foreach ($colors as $key => $color) { // won't work: //$color = strtoupper($color); // works: $colors[$key] = strtoupper($color); } print_r($colors); /* output: Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW ) */ ?> arrays may be parsed by keys through foreach($fooArray as $key=>$value) { // play with $key and its $value } // foreach or via indices for ($i=0;$i<count($fooArray);$i++) { // play with $fooArray[$i]["key"] and $fooArray[$i]["value"] // or just the value with $fooArray[$i] if you used a pure-numeric filling-in like $fooArray[0]=1; $fooArray[1]=2; etc } // for Finally, PHP is not compiled at all (unless you use some semi-compiler like ZEND) but runs very fast, compared to other scripting languages (VBScript for ASP, etc) If you write about accessing "members" of data structures, I think you definitely enter the field of the "object" type in PHP ;-) Regards, |
|||
| By: TheFalklands | Date: 23/03/2003 07:26:00 | Type : Comment |
|
| Hi, >> So if I want a "contsant" array, can I put that in my constants.php file and just include it and I will be able to access it? Yes, you can access it. In functions you need to declare it as global. I hope the answer from VGR is very good explanation. Enjoy! Cheers, |
|||
| By: VGR | Date: 23/03/2003 20:07:00 | Type : Comment |
|
| I think I remember there is no GLOBAL declaration needed for constants. |
|||
| By: TheFalklands | Date: 23/03/2003 23:28:00 | Type : Comment |
|
| Frankly! I had not defined an array as constants(I don't think it can be!). How do you think array is defined as constant? Using define()? Will it work? Thanks |
|||
| By: VGR | Date: 24/03/2003 00:07:00 | Type : Comment |
|
| no, I don't think an array, even hardcoded, can be put in a define statement to become a constant. But this should be tried right out 8-) I was just stating that IMHO, inside the scope of a function, the GLOBAL keyword is not needed to access constants defined globally. So constants and $_SESSION[] are truely global. The rest are "under-globals" |
|||
| By: progGoon | Date: 24/03/2003 00:50:00 | Type : Comment |
|
| VGR, (nothing has been tested, still trying to setup stuff and understand this new found joy of scripting =)). ok last question(well maybe), I have this so far $FORM_ARRAY[5] = array(0 => 'Ttl', 1 => 'Defaults', 2 => 'Title'); $FORM_ARRAY[6] = array(0 => 'BGImage', 1 => 'Defaults', 2 => 'Background Image'); now can I do the following foreach($_POST as $formIndex) { // This is were I am stuck // writeIni($section, $key, $value) // first iteration $formIndex = 'Title' right? writeIni($FORM_ARRAY[$formIndex][0], //should be 'Ttl' $FORM_ARRAY[$formIndex][1], //should be 'Title' // lost here, not too sure about $_POST data // I need to output the value in the form $formIndex); } I am not understanding how to access a multi-dimensional array in this. I hope this makes it a bit more clear what I am trying to do. Main task is to process an entire forms page in one loop. if you want to email me, feel free, we can discuss it via email and I will 'echo' it back up here for record purposes awscrib at earthlink dot net(please let me know if off board discussion are discouraged now, I have not been here in awhile). |
|||
| By: progGoon | Date: 24/03/2003 00:52:00 | Type : Comment |
|
| ugh.....edit....sorry // first iteration $formIndex = 'Title' right? writeIni($FORM_ARRAY[$formIndex][1], //should be 'Defaults' $FORM_ARRAY[$formIndex][2], //should be 'Title' |
|||
| By: progGoon | Date: 24/03/2003 00:54:00 | Type : Comment |
|
| VGR, sorry, just noticed the link you sent from php.net that pretty much describes what I am trying to do, I will let you know, but I think this page will work for refernce Thank you very much! - |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








