tqla Posted February 4, 2011 Share Posted February 4, 2011 Hello. I have an array called $subzz that looks like this array(3) { [0]=> string(1) "3" [1]=> string(1) "2" [2]=> string(1) "1" } I can echo any of the values like this: echo $subzz['2'] But what I am trying to do is look at each value and change another variable accordingly. For example: if $subzz['2'] equals 1 then $var = 100 if $subzz['1'] equals 2 then $var = 50 if $subzz['0'] equals 2 then $var = 5 I suspect I need to use a loop of some kind but I am failing to figure out how to do it. Can someone show me how to tackle this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/ Share on other sites More sharing options...
jcbones Posted February 4, 2011 Share Posted February 4, 2011 if($subzz[2] == 1) { $var = 100; } elseif( $subzz[1] == 2) { $var = 50; } elseif( $subzz[0] == 2) { $var = 5; } Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1169992 Share on other sites More sharing options...
tqla Posted February 4, 2011 Author Share Posted February 4, 2011 I will do that ultimately but the array will change dynamically depending on who is logged in. Sometimes it will look like this: array(3) { [0]=> string(1) "3" [1]=> string(1) "2" [2]=> string(1) "1" } Sometimes it will look like this: array(3) { [0]=> string(1) "2" [1]=> string(1) "1" } Sometimes like this: array(3) { [0]=> string(1) "5" [1]=> string(1) "4" [2]=> string(1) "3" [3]=> string(1) "2" [4]=> string(1) "1" } and so on. It changes depending on who is logged on the website. Is there anyway to cycle through it first to see what's there? Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1169994 Share on other sites More sharing options...
jcbones Posted February 4, 2011 Share Posted February 4, 2011 That information would have resulted in a much better answer. Is the array value tied to the variable value in any way? Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1169995 Share on other sites More sharing options...
tqla Posted February 4, 2011 Author Share Posted February 4, 2011 (yeah, I shuda wrote more, sry) It's not tied to another variable. It's a DB query. But you got me thinking. A "switch" statement will work perfectly here. Thanks jcbones. Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1169996 Share on other sites More sharing options...
drisate Posted February 4, 2011 Share Posted February 4, 2011 Is there anyway to cycle through it first to see what's there? you can cycle through it by doing this foreach($array as $value){ } Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1169998 Share on other sites More sharing options...
jcbones Posted February 4, 2011 Share Posted February 4, 2011 Could try something like: <?php $array = array(3,2,1); //array holding your db values. $variables = array(100,50,5); //array holding your var values, this array should be set up so the key is the expected value of your DB array. foreach($array as $k => $v) { if(array_key_exists($k,$variables)) { $var[] = $variables[$k]; } } //This is all just echoing back to the screen. echo '<pre>'; var_dump($array); var_dump($variables); var_dump($var); $array = $var; //re-assign array to var. echo 'Your array now holds:'; var_dump($array); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1170000 Share on other sites More sharing options...
tqla Posted February 4, 2011 Author Share Posted February 4, 2011 Hey jcbones, I tried your code and got the following. The last 3 arrays are the same. Is this what you were after? array(3) { [0]=> string(1) "3" [1]=> string(1) "2" [2]=> string(1) "1" } array(3) { [0]=> int(100) [1]=> int(50) [2]=> int(5) } array(3) { [0]=> int(100) [1]=> int(50) [2]=> int(5) } Your array now holds: array(3) { [0]=> int(100) [1]=> int(50) [2]=> int(5) } Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1170034 Share on other sites More sharing options...
jcbones Posted February 5, 2011 Share Posted February 5, 2011 Yes, it is just showing you how you can change your DB array (1st array) to list as your desired variable array (last array). Take your DB array, and change up the values, and you will notice that the values in the last array change places. Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1170253 Share on other sites More sharing options...
tqla Posted February 5, 2011 Author Share Posted February 5, 2011 Ah ha. Thanks. I ended up using in_array and then a switch statement but yours was a good lesson. Quote Link to comment https://forums.phpfreaks.com/topic/226715-help-with-array/#findComment-1170307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.