简体   繁体   中英

PHP 7 : Warning: expects parameter 1 to be string, array given

I want to click on a link and convert the $data received in the link.
However, the following error was displayed. It worked fine with PHP 5.x.
Is there any way to do it?
$data needs to be a string, but it becomes an array.
Error

Warning: mb_convert_kana() expects parameter 1 to be string, array given

MbConvertKanaExtension.php


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // mb_convert_kana_option
        if (isset($options['mb_convert_kana_option'])) {
            $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
                $data = $event->getData();

                //ErrorCode
                $data = mb_convert_kana($data, $options['mb_convert_kana_option'], 'UTF-8');
                $event->setData($data);
            });
        }
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'mb_convert_kana_option' => 'aKV',
        ));
    }

twig

<a href="{{ path('ahi_sp_admin_hq_article_index', {"q": {"articleType": "coordinate"}}) }}">

Result of var_dump($data);

array(1) { ["articleType"]=> string(9) "recommend" } NULL string(9) "recommend" string(0) "" NULL NULL NULL

Version
PHP v7.3.25

It appears that $event->getData() returns an associative array.

Based on the dump you showed, if you just want to get the string value of the "articleType" property from it, then simply:

mb_convert_kana($data["articleType"], ...

Also, mb_convert_kana returns a string, so you probably want to put that back into the $data array rather than completely overwriting it. So..

$data["articleType"] = mb_convert_kana($data["articleType"], ...

is probably what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM