kjl-php Posted March 25, 2008 Share Posted March 25, 2008 I think an array is the best choice for this because I don't want to use a db but I could be wrong. I've only done basics in PHP so this is th first time doing anything with arrays. Here's what I have defined: <?php $my_array = array( array(1,'somevalueA','somevalueB'), array(2,'anothervalueA','anothervalueB') ); ?> I want to search through the array for say 1 or 2 or 3 or 4 etc. When it finds the 1 for example I would like to store the value somevalueB in a variable. If I'm looking for 2 I will store anothervalueB in a variable, in essence I will always be looking for the last value (the B value) I've seen a couple of functions but I couldn't figure out what to do. Please tell me the code to use? Thanks in advance. Keith Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/ Share on other sites More sharing options...
Barand Posted March 25, 2008 Share Posted March 25, 2008 Instead of searching for the 1,2 etc, make them the keys <?php $my_array = array( 1 => array('somevalueA','somevalueB'), 2 => array('anothervalueA','anothervalueB') ); // // now update b value for key 2 // $my_array[2][1] = "new_value"; ?> Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500142 Share on other sites More sharing options...
kjl-php Posted March 25, 2008 Author Share Posted March 25, 2008 Thanks for the quick reply. One more question if I can... so in that small array all the values could be referenced this way: $my_array[1][0] would equal somevalueA? $my_array[1][1] would equal somevalueB? $my_array[2][0] would equal anothervalueA? $my_array[2][1] would equal anothervalueB? Would that be how I would grab the values? Thanks again... Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500145 Share on other sites More sharing options...
Barand Posted March 25, 2008 Share Posted March 25, 2008 That's it. To get B value where key is 1 $b = $my_array[1][1]; Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500146 Share on other sites More sharing options...
kjl-php Posted March 25, 2008 Author Share Posted March 25, 2008 Appreciate it....thanks a lot! Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500147 Share on other sites More sharing options...
kjl-php Posted March 25, 2008 Author Share Posted March 25, 2008 It just dawned on me...how would I handle the situation where the key doesn't exist? The key value will be coming in from a querystring and I will be looking for the value in the b column but what will happen if the key doesn't exist? Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500154 Share on other sites More sharing options...
Barand Posted March 25, 2008 Share Posted March 25, 2008 you can check for that with <?php if (!isset($my_array[$key])) { echo "Not found"; } Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500592 Share on other sites More sharing options...
kjl-php Posted March 25, 2008 Author Share Posted March 25, 2008 thanks again Link to comment https://forums.phpfreaks.com/topic/97743-2-dimensional-array-search/#findComment-500607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.