Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    You try something like this:

    PHP Code:
    unset($this->data['phone'][1], $this->data['Phone'][3]); 
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Zangeel View Post
    You try something like this:

    PHP Code:
    unset($this->data['phone'][1], $this->data['Phone'][3]); 
    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
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Well im not sure EXACTLY what you're trying to do, but unsetting is the same weather you have 1 array or 100 arrays within one array. See this example I wrote up

    PHP Code:
    <?php

    // Multi-Dim Array
    $phone = array
    (
        
    => array('111-1111''222-2222'),
        
    => array('333-333''444-4444'),
    );

    //Show all values
    var_dump($phone);

    //Unset the second array piece from the first array value
    unset($phone[1][1]);

    //Show again, now without the '222-2222'
    var_dump($phone);

    ?>
    The out put is this:

    Code:
    First Array Dump:
    
    array
      1 => 
        array
          0 => string '111-1111' (length=8)
          1 => string '222-2222' (length=8)
      2 => 
        array
          0 => string '333-333' (length=7)
          1 => string '444-4444' (length=8)
    
    Second Array Dump after unsetting:
    
    array
      1 => 
        array
          0 => string '111-1111' (length=8)
      2 => 
        array
          0 => string '333-333' (length=7)
          1 => string '444-4444' (length=8)
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts
    PHP Code:
    $new = array();
    foreach (
    $this->data['Phone'] as $key => $var) {

        if (!empty(
    $var['number'])) {
            
    $new[] = $var;
        }
    }
    $this->data['Phone'] = $new
    this seems to work

    Thanx Zangeel.


  •  

    Posting Permissions

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