0

Here is my POST Array PHP

    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

    )

In the above array, I get the book_count. i.e 2 What I want to do is. There will be five array and i need to get the values of array with respect to that of book_count.

In other words - in the above case, the book count is 2, and I need to get

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

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

I need to eliminate the other array(in this case 3 to 5) values irrespective of the values. How can I achieve that?

I have tried

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

but this is eliminating only the null values.

3
  • loop through your array var Commented Jun 11, 2014 at 6:27
  • You can do that simple iteration. See my answer and demo here Commented Jun 11, 2014 at 6:39
  • huseyin - yup, marked and up-voted your answer. thanks Commented Jun 11, 2014 at 6:41

1 Answer 1

1

Demo

You can do that with simple iteration like below;

$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);
Sign up to request clarification or add additional context in comments.

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.