Nathan Cheval
Nathan Cheval

Reputation: 823

Python array in dict

I have a dict with some values needed to use a PHP web service. This web service need an array in that dict of values. So I have this on my Python script :

data = {
        'resId'         : res_id,
        'collId'        : 'letterbox_coll',
        'table'         : 'res_attachments',
        'data'          : {
            'title'             : 'Rapprochement note interne',
            'attachment_type'   : Config.cfg[_process]['attachment_type'],
            'coll_id'           : 'letterbox_coll',
            'res_id_master'     : res_id
        },
        'fileFormat'    : Config.cfg[_process]['format'],
    }

When I print data I have this :

{'resId': '655', 'collId': 'letterbox_coll', 'table': 'res_attachments', 'data': {'title': 'Rapprochement note interne', 'attachment_type': 'outgoing_mail_signed', 'coll_id': 'letterbox_coll', 'res_id_master': '655'}, 'fileFormat': 'pdf'}

But here is the return of my PHP webservice :

Array
(
    [resId] => 655
    [collId] => letterbox_coll
    [table] => res_attachments
    [data] => res_id_master
    [fileFormat] => pdf
)

The php WS use Slim framework with HTTP Request and Response from this framework, here is a piece of code :

use Attachment\models\AttachmentModel;
use Convert\controllers\ConvertPdfController;
use Convert\controllers\ConvertThumbnailController;
use Convert\models\AdrModel;
use Docserver\models\DocserverModel;
use Docserver\models\DocserverTypeModel;
use History\controllers\HistoryController;
use Resource\controllers\ResController;
use Respect\Validation\Validator;
use setasign\Fpdi\TcpdfFpdi;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\CoreConfigModel;
use Resource\controllers\StoreController;
use Template\controllers\TemplateController;
use SrcCore\models\DatabaseModel;
use Resource\models\ResModel;

class AttachmentController
{
    public function create(Request $request, Response $response)
    {
        $data = $request->getParams();
        file_put_contents('/var/www/html/test.txt', print_r($request, true));

So my question is, why PHP retrieve the 'data' index like this ? And how I could send a array ?

Thanks in advance

Upvotes: 0

Views: 58

Answers (1)

delboy1978uk
delboy1978uk

Reputation: 12365

The print_r() command outputs like that. https://www.php.net/manual/en/function.print-r.php

It looks like you HAVE an array. Should you wish to save the array in JSON format, just do the following

$json = json_encode($someArray);

https://www.php.net/manual/en/function.json-encode.php

Upvotes: 2

Related Questions