SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change values in associative array

    Let's say I have an array:

    PHP Code:
    $array = array
    (
      
    'key1' => 'value1',
      
    'key2' => 'value2',
      
    'key3' => 'value3',
      
    'key4' => 'value4',
      
    'key5' => 'value5',
    ); 
    How can I change a specific value in the array? For example, I'd like to change the value for key3:

    PHP Code:
    $array = array
    (
      
    'key1' => 'value1',
      
    'key2' => 'value2',
      
    'key3' => 'SOME NEW VALUE',
      
    'key4' => 'value4',
      
    'key5' => 'value5',
    ); 

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $array['key3'] = 'new value'
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    PHP Code:
    $array['key3'] = 'new value'
    I knew about that. But what if you don't know the key? Can you access the values by numbers, i.e.:

    PHP Code:
    $array[2] = 'new value'
    Sorry for the confusion.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you mean if you don't know the key of the element of which you want to change the value?? How that can happen? If your array is all associative then you need to use the key itself. You cannot go with numbers.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Search the array for the value then change it.

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You may find this to be a better solution:
    PHP Code:
    <?php
    $array 
    = array 

      
    'key1' => 'value1'
      
    'key2' => 'value2'
      
    'key3' => 'value3'
      
    'key4' => 'value4'
      
    'key5' => 'value5'
    );
    $temp array_keys($array);
    echo 
    $array[$temp[2]];
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    foreach(  $array as $k=&$v )
    if( 
    $k == "key3"$v="SOME NEW VAL"
    Untested but should work.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •