refiking Posted June 20, 2011 Share Posted June 20, 2011 I have an array in the $items variable. I need to change one of the values (for state visible). I need to set it to 0. How can I do this? When I print_r($items), here is the result: Array ( [0] => Array ( [id] => 9 [type] => text [ordering] => 13 [published] => 1 [min] => 10 [max] => 100 [name] => State [tips] => Your state [visible] => 1 [required] => 1 [searchable] => 1 [registration] => 1 [options] => [fieldcode] => FIELD_STATE [params] => [value] => florida [access] => 0 [searchLink] => /index.php/field?FIELD_STATE=florida ) [1] => Array ( [id] => 10 [type] => text [ordering] => 14 [published] => 1 [min] => 10 [max] => 100 [name] => City / Town [tips] => Your city or town name [visible] => 1 [required] => 1 [searchable] => 1 [registration] => 1 [options] => [fieldcode] => FIELD_CITY [params] => [value] => palm harbor [access] => 0 [searchLink] => /index.php/field?FIELD_CITY=palm+harbor ) ) Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/ Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 Post the output of the print_r() when it's inside <pre></pre>, please. echo '<pre>'; print_r($items); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232034 Share on other sites More sharing options...
refiking Posted June 20, 2011 Author Share Posted June 20, 2011 Array ( [0] => Array ( [id] => 9 [type] => text [ordering] => 13 [published] => 1 [min] => 10 [max] => 100 [name] => State [tips] => Your state [visible] => 1 [required] => 1 [searchable] => 1 [registration] => 1 [options] => [fieldcode] => FIELD_STATE [params] => [value] => florida [access] => 0 [searchLink] => /index.php/field?FIELD_STATE=florida ) [1] => Array ( [id] => 10 [type] => text [ordering] => 14 [published] => 1 [min] => 10 [max] => 100 [name] => City / Town [tips] => Your city or town name [visible] => 1 [required] => 1 [searchable] => 1 [registration] => 1 [options] => [fieldcode] => FIELD_CITY [params] => [value] => palm harbor [access] => 0 [searchLink] => /index.php/field?FIELD_CITY=palm+harbor ) ) Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232038 Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 $arr['visible'] = 1; where $arr is the name of the variable that you store your array in. Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232041 Share on other sites More sharing options...
refiking Posted June 20, 2011 Author Share Posted June 20, 2011 I don't have access to the script that defines the array. What I want to do is add a conditional statement that if the array contains the state, to change the visible variable to 0. How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232044 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 And you want to change all occurrences of it in the array, right? it isn't completely clear which index you want to check the value of to trigger the change, so you'll need to adjust this to suit your needs. foreach( $items as $k => $v ) { if( array_key_exists('visible', $v) && $v['visible'] == 1 ) { $items[$k]['visible'] = 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232046 Share on other sites More sharing options...
refiking Posted June 20, 2011 Author Share Posted June 20, 2011 I only want to check the array where name => state Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232050 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 You're going to need to clarify exactly what you're after. One array shows name => state and the other shows name => city/town . . . Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232052 Share on other sites More sharing options...
refiking Posted June 20, 2011 Author Share Posted June 20, 2011 I only want to check the array where name => state Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232061 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 So the value is literally 'state'? Then all you need to do is change the array key and value that the foreach() loop is looking for. foreach( $items as $k => $v ) { if( array_key_exists('name', $v) && $v['name'] == 'state' ) { $items[$k]['visible'] = 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/#findComment-1232262 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.