A
A
Alexander2015-12-23 15:42:24
symfony
Alexander, 2015-12-23 15:42:24

Why is it accepted as an array and not as an object?

I pass this string to the controller
{"data":[{"n1":10,"n10":100,"n2":20,"n3":30,"n4":40,"n5":50,"n6 ":60,"n7":70,"n8":80,"n9":90,"s1":"aaa","s2":"bbb"},{"n1":101,"n10": 1001,"n2":201,"n3":301,"n4":401,"n5":501,"n6":601,"n7":701,"n8":801,"n9":901, "s1":"ccc","s2":"ddd"},{"n1":102,"n10":100,"n2":202,"n3":30,"n4":402,"n5 ":50,"n6":60,"n7":70,"n8":80,"n9":90,"s1":"eee","s2":"fff"}],"baseclass": "Custom","classlibrary":"claass","comment":"","controlcount":0,"height":17,"helpcontextid":0,"left":0,"name":"Myarray", "nsize":3,"parentclass":"Custom","picture":"","tag":"","top":0,"whatsthishelpid":-1,"width":100}
The controller itselfwidth":100} The controller itselfwidth":100} The controller itself

/**
   * @param ParamFetcher $paramFetcher Paramfetcher
     *
     * @RequestParam(name="data", nullable=false, strict=true, description="Data.", array=true)
     *
     * @return View
     */
    public function postDocsAction(ParamFetcher $paramFetcher)
      $data       = $paramFetcher->get('data');
        $logger     = $this->get('logger');
        $i=0;
        foreach ($data as $key => $d) {
            $i++;
            $logger->info($key.'=='.gettype($d));
        }

gettype($d) is an array, not an object

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-12-23
@alexey-m-ukolov

I am not an expert in Symfony, of course, but judging by the array=true in DocBlock, it is indicated that json_decode should return an array, not an object.

A
Anton Natarov, 2015-12-23
@HanDroid

What did you do to make it an object?
The array goes pair + value. PHP can convert data types.
But an array is an array, and an object is an object. In the loop, you only iterate over the received.
You either create an object and fill it with the resulting array. Or a more elegant solution will be prompted to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question