3

I want to get value of post array in yii2, I have array like this

[Brand] => Array
                (
                    [name] => Array
                        (
                            [0] => testing
                            [1] => jkhkjhjkhjk
                        )

                    [tagline] => Array
                        (
                            [0] => kjhjkh
                            [1] => 
                        )

                    [meta_keyword] => Array
                        (
                            [0] => 
                            [1] => 
                        )

                    [sort_order] => 
                    [image] => brand/1452498338552.jpg
                    [status] => 
                )

        )

I tried to get value with below function, but i am unable to get it.

$request = Yii::$app->request;

$request->post('Brand[name][0]');

How can i get value of name array ? I don't want to use it like $_POST['Brand']['name'][0], I need to use only yii2 function

1 Answer 1

7

Try to use ArrayHelper class

$var = ArrayHelper::getValue($request->post(), 'Brand.name.0');

The way $request->post() method works, it just returns to you a value from $_POST, so usage is:

$brand = $request->post('Brand'); // now $brand variable contains $_POST['Brand']
$var = $brand['name'][0]
Sign up to request clarification or add additional context in comments.

1 Comment

Would Array ( [total_images] => 4 [images_uploaded] => 1 ) these two value can be returned from the same method ?

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.