SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2008
    Location
    HidePoint
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unset in Multidementional Array

    i have an array $this-data['Phone'] ( am using cakephp framework)

    output of this give this.

    Code:
    Array
    (
        [0] => Array
            (
                [number] => 23467887
            )
    
        [1] => Array
            (
                [number] => 
            )
    
        [2] => Array
            (
                [number] => 23563866
            )
    
        [3] => Array
            (
                [number] => 
            )
    
        [4] => Array
            (
                [number] => 11222
            )
    
    )
    i wanna unset the [1] and [3] because it doesnt have any values for the number.

    i want a an array returned like this

    Code:
    Array
    (
        [0] => Array
            (
                [number] => 23467887
            )
    
    
        [1] => Array
            (
                [number] => 23563866
            )
    
    
        [2] => Array
            (
                [number] => 11222
            )
    
    )
    Note: the key values have changed


    After the all the manipulation all this should be stored in $this->data['Phone']

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    unset($foo[1], $foo[3]);
    $foo array_values($foo); 

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2008
    Location
    HidePoint
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    PHP Code:
    unset($foo[1], $foo[3]);
    $foo array_values($foo); 
    thanx for the fast reply. the [1] [3] was jsut an example.

    incase any of the [number] is not entered in the form i wanna unset it.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would just use a for() loop to walk through each element in the array and test it's value. If it's not something you want, you can unset($foo[$i]).

    You could also use array_filter()

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2008
    Location
    HidePoint
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanx a lot.


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
  •