Jump to content

Help With Changing A Value In Array


refiking

Recommended Posts

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 ) ) 

Link to comment
https://forums.phpfreaks.com/topic/239849-help-with-changing-a-value-in-array/
Share on other sites

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
        )

)

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;
   }
}

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;
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.