Ask for an empty record in a multidimensional array

Source: Internet
Author: User
Want to let empty records are deleted, and after deletion, some key is not, to the smooth key is also rearranged
For example, delete the $arr [' 0 '] $arr [' 3 '] $arr [' 5 '], then the finishing can be $arr [' 0 '] $arr [' 1 '] $arr [' 2 '] $arr [' 3 '] $arr [' 4 '] $arr [' 5 '] ...
Of course, the picture is a two-dimensional array, is to let the deletion, the subsequent key sequence also follow up


Using the online method is not good to make
function Array_no_empty ($arr) {
if (Is_array ($arr)) {
foreach ($arr as $k = = $v) {
if (empty ($v)) unset ($arr [$k]);
ElseIf (Is_array ($v)) {
$arr [$k] = Array_no_empty ($v);
}
}
}
return $arr;
}

It's useless, isn't it? Always say the middle array_no_empty undefined or something.

There's no other way, thank you.


Reply to discussion (solution)

Your Array_no_empty function is available
But something is wrong: An empty member array is returned

$a = Array (' a ' = =  0, ' b ' = = '),  array (' a ' = = 0, ' b ' = = '),  array (' A ' = 1, ' b ' = ' 2 ') ,);p Rint_r (Array_values (Array_no_empty ($a))), function Array_no_empty ($arr) {  if (Is_array ($arr)) {    foreach ($arr as $k = $v) {      if (empty ($v)) unset ($arr [$k]);      ElseIf (Is_array ($v)) {        $arr [$k] = Array_no_empty ($v);}}  }  return $arr;}
Array (    [0] = = Array (        )    [1] = = Array (        )    [2]        = = Array (            [A] = > 1            [b] = 2        ))

Modified a bit
$a = Array (' a ' = =  0, ' b ' = = '),  array (' a ' = = 0, ' b ' = = '),  array (' A ' = 1, ' b ' = ' 2 ') ,);p Rint_r (Array_no_empty ($a)), function Array_no_empty ($arr) {  if (Is_array ($arr)) {    foreach ($arr as $k = = $v) {      if (empty ($v)) unset ($arr [$k]);      ElseIf (Is_array ($v)) {        $t = Array_no_empty ($v);        if ($t) $arr [$k] = $t;        else unset ($arr [$k]);  }} return $arr;}
Array (    [2] = = Array        (            [A] = 1            [b] = 2        ))

rearrange subscript available Array_values functions
$a = Array (' a ' = =  0, ' b ' = = '),  array (' a ' = = 0, ' b ' = = '),  array (' A ' = 1, ' b ' = ' 2 ') ,);p Rint_r (Array_values (Array_no_empty ($a))), function Array_no_empty ($arr) {  if (Is_array ($arr)) {    foreach ($arr as $k = $v) {      if (empty ($v)) unset ($arr [$k]);      ElseIf (Is_array ($v)) {        $t = Array_no_empty ($v);        if ($t) $arr [$k] = $t;        else unset ($arr [$k]);  }} return $arr;}
Array (    [0] = = Array        (            [A] = 1            [b] = 2        ))
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.