php陣列問題 (抽除空值)

有一個ㄧ維陣列a,長度不設限,有可能是1~100任意數

這邊假定陣列a長度是五個元素,但有兩個是空值,如下

$a = array("v1","v2","","","v3");

$a = array("v1","","v2","v3","");

我要怎麼把空值的部份抽離讓陣列a變成

$a = array("v1","v2","v3");

已更新項目:

我有想過用 array_shift() 跟 array_push(),但不太會寫...請幫忙

2 個已更新項目:

有PHP的解答方式嗎??

1 個解答

評分
  • 1 0 年前
    最佳解答

    <html>

    <head>

    <script language="javascript">

    function a(){

    var c=new Array("v1","v2","","","v3");

    var c1=new Array();

    var num=0;

    for(var i=0;i<c.length;i+=1){

    if(c[i]!=""){

    c1[num]=c[i];

    ++num;

    }

    }

    for(var x=0;x<c1.length;x+=1){

    document.write(c1[x]+"<br>");

    }

    }

    </script>

    </head>

    <body onload=a()>

    </body></html>

    2008-08-03 01:12:58 補充:

    以上全形記得改半形.....

    2008-08-03 01:13:26 補充:

    c1陣列就是你需要的答案~

    2008-08-03 18:51:16 補充:

    $c=Array("v1","v2","","","v3");

    $c1=Array();

    $num=0;

    for($i=0;$i

    2008-08-03 18:53:11 補充:

    for($i=0 ; $i<count($c);$i++){

    if($c[$i]!=""){

    $c1[$num]=$c[$i];

    $num+=1;

    }

    }

還有問題?馬上發問,尋求解答。