2014-06-11 66 views
0

這是帖子陣列PHP處理POST ARRAY和array_filter

Array 
    (

    [book_count] => 2 

    [book_code_1] => 230 
    [book_name_1] => 20 

    [book_code_2] => 210 
    [book_name_2] => 40 

    [book_code_3] => 
    [book_name_3] => 60 

    [book_code_4] => 
    [book_name_4] => 80 

    [book_code_5] => 
    [book_name_5] => 90 

    ) 

在上面的陣列中,我得到的共BOOK_COUNT。即2 我想要做的是。 將有五個數組,我需要得到相對於book_count數組的值。

換言之 - 在上述情況下,書數爲2,並且我需要得到

[book_code_1] => 230 
    [book_name_1] => 20 

    [book_code_2] => 210 
    [book_name_2] => 40 

我需要不論消除另陣列(在這種情況下3至5)的值值。我怎樣才能做到這一點?

我已經試過

<?php $new_array = array_filter($_POST) ?> 

但這只是消除了空值。

+0

通過你的數組循環var –

+0

你可以做那簡單的迭代。看到我的答案和演示[這裏](http://stackoverflow.com/questions/24155952/handling-post-array-in-php-and-array-filter/24156161#24156161) –

+0

huseyin - yup,投了你的答案。謝謝 – user3350885

回答

1

Demo

你可以做到這一點簡單的迭代像下面;

$arr = array(

    "book_count" => 2, 

    "book_code_1" => 230, 
    "book_name_1" => 20, 

    "book_code_2" => 210, 
    "book_name_2" => 40, 

    "book_code_3" => "", 
    "book_name_3" => 60, 

    "book_code_4" => "", 
    "book_name_4" => 80, 

    "book_code_5" => "", 
    "book_name_5" => 90 

    ); 

$result = array(); 
for ($i = 1; $i <= $arr["book_count"]; $i++) { 
    $result["book_code_" . $i] = $arr["book_code_" . $i]; 
    $result["book_name_" . $i] = $arr["book_name_" . $i]; 
} 

var_dump($result); 
+0

是的,謝謝..這就是我想要的。謝謝哥們 ;) – user3350885