创建包含数组和设置Key值的JSON


Creating JSON which contains arrays and setting Key values

php输出的Json包含3个数组,eventsid, TipsTB &TipsTW当我将json传递到我的html文件时,它显示了键为0和1的数组值。如何将密钥设置为唯一的,如tip1和tip2?

我创建了数组,然后将其编码为JSON,如下所示

function createJson($eventIds, $TipsTB, $TipsTW, $status) {
              $jsonData = new stdClass();
              $jsonData->source = "Betting Tips";
              $jsonData->published = date('Y-m-d H:s:i;');
              $jsonData->status = $status;
              $jsonData->eventIDs = $eventIds;
              $jsonData->TipsTB = $TipsTB;
              $jsonData->TipsTW = $TipsTW;
              return $jsonData;
    }
              echo json_encode($jsonData);

Object {source: "Betting Tips", published: "2015-05-21 12:54:56;", status: true, eventIDs: Array[2], TipsTB: Array[2], TipsTW: Array[0]}

我如何制作除0,1,2,3…以外的3个数组的键?

$jsonData设置为数组而不是stdClass。然后将数据分配给该数组的键。它应该可以工作

$jsonData = array();
$jsonData['foo'] = $TipsTB;
$jsonData['bar'] = 42;
echo json_encode($jsonData);