【问题标题】:htmlspecialchars() expects parameter 1 to be string, array laravel?htmlspecialchars() 期望参数 1 是字符串,数组 laravel?
【发布时间】:2025-12-08 22:00:02
【问题描述】:

执行 foreach 时会出现此错误:

helpers.php 第 532 行中的错误异常: htmlspecialchars() 期望参数 1 是字符串,给定数组

array:90 [▼
      1 => array:5 [▼
                    "users_responsavel_id" => 2
                    "users_responsavel_nome" => "Freitas"
                    "users_responsavel_cgu_cpf" => "1185420"
                    "status" => 1
                    "numero_chave" => "13"
                   ]
      2 => 14
      4 => array:5 [▼
                    "users_responsavel_id" => 4
                    "users_responsavel_nome" => "Ana virgulina"
                    "users_responsavel_cgu_cpf" => "545687"
                    "status" => 1
                    "numero_chave" => "15"
                   ]
      6 => array:2 [▼
                    "chave_invalida_numero" => 16
                    "chave_invalida_status" => true
                   ]
      7 => 17
      8 => 18
      9 => 19
      11 => array:2 [▼
                     "chave_invalida_numero" => 20
                     "chave_invalida_status" => true
                    ]
      13 => array:2 [▼
                     "chave_invalida_numero" => 21
                     "chave_invalida_status" => true
                    ]
      22 => 26
      23 => 27
      24 => 28
      25 => 29
      26 => 30
     ]


<div class="card-deck" id="chaves_geradas">
                @foreach($b_c_d_c_a as $key => $value)
                    @if (is_array($value) || is_object($value))
                        @foreach ($value as $key_obj => $value_obj)
                            @if (in_array($key_obj == 'chave_invalida_numero', $value))
                            <div class="card mb-3 text-center card-inverse card-primary-transparent" style="line-height: 0;padding-top: 20px;padding-bottom: 5px;color: rgb(1, 255, 116);background-color: rgb(38, 50, 56);cursor: not-allowed;" data-toggle="tooltip" data-html="true" data-placement="top" title="Chave inválida.">
                                <div class="card-block">
                                    <span class="fa-stack" style="font-size: 45px;margin-top: -30px;margin-right: -20px;margin-bottom: -25px;x;margin-left: -20px;">
                                        <div class="fa-stack-1x" style="font-size: 65px;margin-top: -5px;">
                                        {{ $value_obj }}  
                                        </div>
                                    </span>
                                </div>
                            </div>
                            @elseif (in_array($key_obj == 'numero_chave', $value))
                            <div class="card mb-3 text-center card-inverse card-primary-transparent" style="line-height: 0;padding-top: 20px;padding-bottom: 5px;color: rgb(1, 255, 116);background-color: rgb(38, 50, 56);cursor: not-allowed;" data-toggle="tooltip" data-html="true" data-placement="top" title="Chave com usuário.">
                                <div class="card-block">
                                    <span class="fa-stack" style="font-size: 45px;margin-top: -30px;margin-right: -20px;margin-bottom: -25px;x;margin-left: -20px;">
                                        <div class="fa-stack-1x" style="font-size: 65px;margin-top: -5px;">
                                        {{ $value_obj }}
                                        </div>
                                    </span>
                                </div>
                            </div>
                            @endif
                        @endforeach

                    @else

                        <div class="card mb-3 text-center card-inverse card-primary-transparent" style="line-height: 0;padding-top: 20px;padding-bottom: 5px;">
                            <div class="card-block n_chaves" numero_chave="{{ $value }}">
                            <p style="font-size: 65px;">{{ $value }}</p>
                            </div>
                        </div>

                    @endif
                @endforeach
            </div>

我正在生成这些对象来管理扇区中的一组键。

有人可以帮我找到一种方法来执行此代码吗?

【问题讨论】:

  • 所以你把键 chave_usuario_responsavel 留在了 foreach 中,这会在 else 条件下执行

标签: php arrays laravel htmlspecialchars


【解决方案1】:

是的,您已经嵌套了数组,但 htmlspecialchars 需要字符串。

在来自https://gist.github.com/igorw/7628042 的递归array_map 下方,可以将htmlspecialchars 应用于嵌套数组。

function array_map_recursive($f, $xs) {
    $out = [];
    foreach ($xs as $k => $x) {
        $out[$k] = (is_array($x)) ? array_map_recursive($f, $x) : $f($x);
    }
    return $out;
}

$data = [
    'foo' => [
        'bar' => [
            'baz' => [
                'lorem ipsum'
            ]
        ]
    ]
];

print_r(array_map_recursive('htmlspecialchars', $data));

【讨论】:

  • 我不明白如何在我的情况下申请。你能解释一下如何在我的刀片上应用这个数组吗?
  • 好的,你可以用你的数组填充$data。我想在将数组传递给刀片视图之前,最好在控制器方法中执行此操作。
  • 这样,我一次得到一个对象。 php @elseif (in_array($key_obj == 'numero_chave', $value)) {{ $value_obj }}@endif 返回“13”
  • 我想访问其他对象。 "users_responsavel_nome" => "弗雷塔斯" || "users_responsavel_cgu_cpf" => "1185420"