add data into matriz php

I´m working with laravel 10. I need add data into this structure:

$json = [
            "groupCode" => $groupCode,
            "title" => "Solicitud de Firma",
            "description" => "Solicitud de Firma - Descripción",
            "customization" => [
                "requestMailSubject" => "Mi Email Subject",
                "requestMailBody" => "Mi Email Body",
                "requestSmsBody" => "SMS Body"
            ],
            "recipients" => [
                [
                    "key" => "firmante01",
                    "presential" => true
                ]
            ],
            "messages" => [
                [
                    "document" => [
                        "templateType" => "base64",
                        "templateCode" => $templateCode, //"564FDSA4765",
                        "templateReference" => $pdfInBase64,
                        "autoFinalize" => true,
                        "formRequired" => true,
                        //"items" => $items,
                    ],
                    "policies"  => [
                        "attachments" => [], // it´s filled below
                    ],
                    "metadataList" => $metaDataList,
                    "callbackURL" => "",
                    "callbackAuthorization" => "Basic " . "viafirmaapi" . ":" . "f1rm4v1420$",
                    "callbackMails" => "",
                    "callbackMailFilename" => "PRECONTRATO_" . $precontract["insert_id"]["precontract_id"] . ".pdf"
                ],
            ],
        ];

In this array, i need add data dynamically into:

"policies"  => [
                "attachments" => [], // it´s filled below
            ],

To do this, i´m trying:

$precontractDocument = PrecontractAttachment::where('precontract_id', $precontract["insert_id"]["precontract_id"])->get();
        $attachments = [];
        $document = [];
        $tokens = [];
        for ($i = 0; $i < count($precontractDocument); $i++) {
            array_push($tokens, $this->getSecurityTokensViaFirma());
            $attachments["type"] = "PDF";
            $attachments["helpText"] = "Documento adjunto";
            $attachments["fileReference"] = $tokens[$i];
            $attachments["fileName"] = $precontractDocument[$i]["filename_result"];
            $attachments["readOnly"] = true;
            array_push($document, $attachments);
            $this->uploadDocumentViafirma($document, $tokens, $precontract["insert_id"]["precontract_id"]);
        }
        // add items document into metadatalist
        $json["messages"]["policies"]["attachments"] = $document;

But this code, only add one document i need any documents. To try add any documents, i´m doing:

// add items document into metadatalist
        array_push($json["messages"]["policies"]["attachments"], $document);

But return local.ERROR: array_push(): Argument #1 ($array) must be of type array, null given

I don´t know how i can to do, to add data into

"messages" => [
                [
    "policies"  => [
                            "attachments" => [], // it´s filled below
                        ],
    ]
    ]

Anybody can help me to solve my problem?

Thanks for readme and sorry for my bad english.

>Solution :

Try this

array_push($json["messages"][0]["policies"]["attachments"], $document);

or update $json array with:

        $json = [
            "groupCode" => $groupCode,
            "title" => "Solicitud de Firma",
            "description" => "Solicitud de Firma - Descripción",
            "customization" => [
                "requestMailSubject" => "Mi Email Subject",
                "requestMailBody" => "Mi Email Body",
                "requestSmsBody" => "SMS Body"
            ],
            "recipients" => [
                [
                    "key" => "firmante01",
                    "presential" => true
                ]
            ],
            "messages" => [
                "document" => [
                    "templateType" => "base64",
                    "templateCode" => $templateCode, //"564FDSA4765",
                    "templateReference" => $pdfInBase64,
                    "autoFinalize" => true,
                    "formRequired" => true,
                    //"items" => $items,
                ],
                "policies"  => [
                    "attachments" => [], // it´s filled below
                ],
                "metadataList" => $metaDataList,
                "callbackURL" => "",
                "callbackAuthorization" => "Basic " . "viafirmaapi" . ":" . "f1rm4v1420$",
                "callbackMails" => "",
                "callbackMailFilename" => "PRECONTRATO_" . $precontract["insert_id"]["precontract_id"] . ".pdf"
            ],
        ];

Leave a Reply