0

I have an array that I got from my $_POST data with this values

Array ( 
    [unit] => 
    [items] => Array ( 
        [1] => Array ( 
            [category] => 1 
            [items] => 5 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] =>
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
        [2] => Array ( 
            [category] => 1 
            [items] => 20 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] => 
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
        [3] => Array ( 
            [category] => 1 
            [items] => 27 
            [qty] => 1 
            [jan] => 1 
            [feb] => 
            [mar] => 
            [apr] => 
            [may] => 
            [jun] => 
            [jul] => 
            [aug] => 
            [sep] => 
            [oct] => 
            [nov] => 
            [dec] => 
        ) 
    ) 
    [action] => 
)

I'm trying to get each array and pass it to my model for database insertion. For example get the array of [items] where [1] points.

I have tried using

$array_col = array_column($_POST, 'items');
print_r($array_col);

but it returns Array() which is empty.

Thank you for the answers.

3 Answers 3

1

I suspect that your data are in $_POST['items']. so:

$array_col = $_POST['items'];

Then, to iterate through them you need a loop.

foreach($array_col as $col){
  // Do your stuff here
  print_r($col);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but I want to get each $_POST['items'] individually. Like the [1] first then [2] and so on...
1

From PHP documentation ...
http://php.net/manual/en/function.array-column.php
array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

Check you have array key index - 'items' are in various levels (level-0 and also in level 2)

Comments

0

You can use this code.

foreach($_POST['items'] as $a){
    // Code will go here. Whatever.
    print_r($a);
}

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.