Create associative array with other arrays in PHP to use as return JSON

0

As an example, I'm going to use what I've been given quite simply, this Array will be obtained with data coming from a query of BD :

$S1 = [[0,100], [1,200], [2,700]];
$S2 = [[0,700], [1,300], [2,400]];
$Label = [[0,SPI], [1,MVA], [2,ITB]];

I need to create a single Array with this information to get a JSON return, but I do not understand how to do it and I am not able to associate Arrays and the attempts I made were strange, the ending was not correct.

    
asked by anonymous 22.02.2018 / 13:13

2 answers

1

I believe this is your answer:

<?php 
    header('Content-Type: application/json');
    $S1 = [[0,100], [1,200], [2,700]];
    $S2 = [[0,700], [1,300], [2,400]];
    $Label = [[0,'SPI'], [1,'MVA'], [2,'ITB']];

    $retorno = array('S1' => $S1, 'S2' => $S2, 'Label' => $Label);
    echo json_encode($retorno);
?>
    
22.02.2018 / 13:29
1

I believe the statements in these arrays are wrong .. so I'll consider the correct one to be this way:

$S1 = [[0,100], [1,200], [2,700]];
$S2 = [[0,700], [1,300], [2,400]];
$Label = [[0,'SPI'], [1,'MVA'], [2,'ITB']];

$arr = [
        'S1' => $S1,
        'S2' => $S2,
        'Label' => $Label
    ];

var_dump(json_encode($arr));
    
22.02.2018 / 13:28