SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2014
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Remove elements from arrays

    hi

    I am having a multidimensional array in $a.

    i get this result after using print_f

    PHP Code:
    Array (
     [
    0] => Array ( [sno] => [num1] => 45 [num2] => 45 [result] => 2025 )
     [
    1] => Array ( [sno] => 17 [num1] => 34 [num2] => 36 [result] => 1224 )
     [
    2] => Array ( [sno] => [num1] => 78 [num2] => [result] => 390 )

    using foreach and converting into individual arrays
    PHP Code:
    foreach($a as $key => $value)
        {
            
    print_r($key $value); echo "<br/>";
             }

    and 
    the result is

    Array ( [sno] => [num1] => 45 [num2] => 45 [result] => 2025 )
    Array ( [
    sno] => 17 [num1] => 34 [num2] => 36 [result] => 1224 )
    Array ( [
    sno] => [num1] => 78 [num2] => [result] => 390 
    now i want to remove num1 and num2 elements from each array..

    so that the resulting array contains 2 elements sno and result

    i tried unset() but not getting result. any help

  2. #2
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    159
    Mentioned
    3 Post(s)
    Tagged
    2 Thread(s)
    You should be able to use "unset" like this:

    Code:
    $array = array("sno" => "sno-val", "num1" => "num1-val");
    print_r($array);
    
    unset($array['num1']);
    print_r($array);
    Can you show us the code with the "unset" you tried?

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,904
    Mentioned
    74 Post(s)
    Tagged
    7 Thread(s)
    Try this:

    PHP Code:

    $x 
    = array 
    (
        array( 
    'sno' =>  4'num1' => 45'num2' => 45'result' => 2025),
        array( 
    'sno' => 17'num1' => 34'num2' => 36'result' => 1224),
        array( 
    'sno' =>  2'num1' => 78'num2' =>  5'result' =>  390),
    );

    echo 
    '<pre>';
      echo 
    '<br />Before: ';
       
    print_r($x);

      
    // Remove 'num1' && 'num2' 
        
    foreach($x as $key => $value)
        {
          foreach(
    $x[$key] as $key2 => $value2)
          {
            if(
    'num1'===$key2 || 'num2'===$key2 )
            {
              unset( 
    $x[$key][$key2] ); 
              
    // echo '<br />', $key2, ', ', $value2;  
            
    }  
          
    // print_r( $key = $value); 
          // echo '<br/>';
          
    }  
        }

       echo 
    '<hr />';
       echo 
    '<br />After: '
       
    print_r($x);

    echo 
    '</pre>'


    Output:
    Code:
    Before: Array
    (
        [0] => Array
            (
                [sno] => 4
                [num1] => 45
                [num2] => 45
                [result] => 2025
            )
    
        [1] => Array
            (
                [sno] => 17
                [num1] => 34
                [num2] => 36
                [result] => 1224
            )
    
        [2] => Array
            (
                [sno] => 2
                [num1] => 78
                [num2] => 5
                [result] => 390
            )
    )
    
    After: Array ( [0] => Array ( [sno] => 4 [result] => 2025 ) [1] => Array ( [sno] => 17 [result] => 1224 ) [2] => Array ( [sno] => 2 [result] => 390 ) )
    Last edited by John_Betong; Apr 26, 2014 at 10:27. Reason: Formatting :(
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


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
  •