0

When doing the foreach this error occurs:

ErrorException in helpers.php line 532: htmlspecialchars() expects parameter 1 to be string, array given

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>

I am generating these objects to manage a set of keys in a sector.

Can someone help me find a way to do this code.

1
  • So you left the key chave_usuario_responsavel in foreach, this gets executed in else condition Commented May 30, 2017 at 9:33

1 Answer 1

2

Yes, you've nested arrays, but htmlspecialchars expects string.

Below a recursive array_map from https://gist.github.com/igorw/7628042 which makes it possible to apply htmlspecialchars to a nested array.

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

4 Comments

I did not understand how to apply in my situation. Could you please explain how to apply with this array on my blade.
Ok, you can fill $data with your array. I suppose it's better to do this in your controller method before passing the array to your blade view.
The way it is, I get one object at a time. php @elseif (in_array($key_obj == 'numero_chave', $value)) {{ $value_obj }}@endif return "13"
I want to access the other objects. "users_responsavel_nome" => "Freitas" || "users_responsavel_cgu_cpf" => "1185420"

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.