Remove elements from arrays

hi

I am having a multidimensional array in $a.

i get this result after using print_f

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 )
)

using foreach and converting into individual arrays


foreach($a as $key => $value)
	{
		print_r($key = $value); echo "<br/>";
         }

and the result is

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 )

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

You should be able to use “unset” like this:

$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?

Try this:



$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:



[B]Before:[/B] 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
        )
)
[HR][/HR]
[B]After:[/B] Array
(
    [0] => Array
        (
            [sno] => 4
            [result] => 2025
        )

    [1] => Array
        (
            [sno] => 17
            [result] => 1224
        )

    [2] => Array
        (
            [sno] => 2
            [result] => 390
        )
)