SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member nishu123's Avatar
    Join Date
    Aug 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace string in an array

    Hi,
    any one please help to convert this first array to second array
    First Array:-
    Array
    (
    [items] => Array
    (
    [0] => Array
    (
    [Id] => 7
    [Name] => server1
    )

    [1] => Array
    (
    [Id] => 17
    [Name] => Test1
    )

    )

    );
    Second Array :-
    Array(
    'items' => Array
    (
    '0' => Array
    (
    'Id' => '7',
    'Name' => 'server2'
    ),

    '1' => Array
    (
    'Id' => '17',
    'Name' => 'Test2'
    )

    )

    );

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you provide a little more information as to what you are trying to accomplish?
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $x is your first array
    $y is your second array

    PHP Code:
    $x = array(
                
    'items' => array(
                                    
    0    =>    array(id => '7',    name => 'server1'),
                                    
    1    =>    array(id => '17',    name => 'test1')
                                ),
            );
            
    $y = array(
                
    'items' => array(
                                    
    0    =>    array(id => '7',    name => 'server2'),
                                    
    1    =>    array(id => '17',    name => 'test2')
                                ),
            ); 
    first let's check what's inside your arrays
    PHP Code:
    print_r($x['items']);
    echo 
    '<br />';
    print_r($y['items']); 
    returns
    Code:
    Array ( [0] => Array ( [id] => 7 [name] => server1 ) [1] => Array ( [id] => 17 [name] => test1 ) )
    Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
    now let's perform your task - convert first array to second array
    PHP Code:
    for($i=0$i<count($x['items'][0]); $i++){
        foreach(
    $x['items'][$i] AS $key => $value){
            
    $x['items'][$i][$key] = $y['items'][$i][$key];
        }

    let's check what are the new values of your arrays
    PHP Code:
    print_r($x['items']);
    echo 
    '<br />';
    print_r($y['items']); 
    returns
    Code:
    Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
    Array ( [0] => Array ( [id] => 7 [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) )
    task accomplished

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RNEL View Post
    task accomplished
    Unfortunately it was using assignment, rather than a string replacement as was hinted at by the subject line: replace string in an array

    Perhaps a look at the documentation for preg_replace will be of some help.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when you assign a new value to an array you are already replacing it

    PHP Code:
    $x['items'][$i][$key] = preg_replace('/'.$x['items'][$i][$key].'/i'$y['items'][$i][$key], $x['items'][$i][$key]); 
    and

    PHP Code:
    $x['items'][$i][$key] = $y['items'][$i][$key]; 
    both returns
    PHP Code:
    Array ( [0] => Array ( [id] => [name] => server2 ) [1] => Array ( [id] => 17 [name] => test2 ) ) 
    which makes the task accomplished

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RNEL View Post
    which makes the task accomplished
    It'll be interesting to see what nishu123 makes of this.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member nishu123's Avatar
    Join Date
    Aug 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou dear.
    My first array item is displayed as [items] and In second array displayed as 'items'. can we replace [] in to '' in whole array?
    please...

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    nishu123, do you really want the output converting like that (I only ask because it seems an odd request to change the data and output in that strange format)? In which case, can you show us how you are getting the original output (it looks like from print_r)?
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Member nishu123's Avatar
    Join Date
    Aug 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,
    I worte a function it will return JSON. I will decode this by using json_decode, so i will get the result like First Array. am i right? But I need the array format like second Array. ie, [0] to '0' and [items] to 'items' like that.

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nishu123 View Post
    hi,
    I worte a function it will return JSON. I will decode this by using json_decode, so i will get the result like First Array. am i right? But I need the array format like second Array. ie, [0] to '0' and [items] to 'items' like that.
    With JavaScript, it doesn't matter if numeric indexes are referenced as integers or as strings.

    For example:

    Code javascript:
    var arr = [];
    arr[4] = 'item 1';
    arr['5'] = 'item 2';

    Both arr[4] and arr['4'] can be used to access 'item 1', and similarly, arr[5] and arr['5'] will access 'item 2'
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, i didn't know this about javascript. thnx


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
  •