0
Array
(
    [0] => Array
        (
            [accountNo] => 208773

        )

)
Array
(
    [0] => Array
        (
            [accountNo] => 9415238

        )

)
Array
(
)

how can i unset the last array so that it must display only first 2 array.

please help

thanks

6
  • What code produced the above output? What variable(s) are you printing here? Commented May 6, 2011 at 5:18
  • $sql = "SELECT * FROM members WHERE profilenam = '".trim($arrUsr[$indxUser])."'"; $result =execute_query($sql,false); execute_query gives me result one-by-one Commented May 6, 2011 at 5:21
  • is there only one element in each array?? Commented May 6, 2011 at 5:21
  • do u want this output? codepad.org/4tHSKE5i Commented May 6, 2011 at 5:33
  • @diEcho , i dont want the account field i just want to remove the last array Commented May 6, 2011 at 5:37

2 Answers 2

2

If these 3 arrays are the content of one array, let's call it $array:

array_pop($array);

Will remove the last one, and optionally return it's value.

array_pop — Pop the element off the end of array

http://php.net/manual/function.array-pop.php


This does the same thing as unset() here, but for curiosity's sake, here's another way:

// Move the pointer to the last element
end($array);

// Get the key of the element
$key = key($array);

// Unset the item
unset($array[$key]);

Just use array_pop() though, the other method was for entertainment purposes only, but you could use it if you want to change the last element's value.

Demo: http://codepad.org/UFjal89X

Some reference:

key(): http://php.net/manual/function.key.php

end(): http://php.net/manual/function.end.php

Sign up to request clarification or add additional context in comments.

Comments

0

try this ( if i understand your problem)

 $output =array();
 foreach($input as $k=>$v){
    if(!empty($v)){ 
        $output[$k]=$v;
    }
}

WORKING DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.