第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

從 PHP 創(chuàng)建 JS 數(shù)組

從 PHP 創(chuàng)建 JS 數(shù)組

PHP
MYYA 2023-04-28 14:24:32
我正在開發(fā) Google Geochart,我希望它是動(dòng)態(tài)的。這是使數(shù)組存儲(chǔ)兩個(gè)值的靜態(tài)代碼如下。國家是地圖的唯一標(biāo)識(shí)符,顏色值是地圖的顏色。(無關(guān)但上下文)  arrayData = [['Country','Color Value'],  ['China',3],  ['Russia',2.6],  ['France',2.5],  ['Spain',2.4],  ['Portugal',1.1]我希望能夠從取自 php echo 值的 for 循環(huán)中創(chuàng)建此數(shù)組。像這樣的偽代碼;for (i in range table.length) arrayData = [['Country','Color Value'], [<?php echo $statements["Country"]; ?>], [<?php echo $statements["Color_Value"]; ?>], 與獲取所有值的 php 表非常相似,我想做同樣的事情,但對(duì)于 JavaScript 數(shù)組。任何建議都會(huì)很好!編輯:靜態(tài)代碼是一個(gè)例子,我想從數(shù)據(jù)庫中獲取值,第一個(gè)值是國家,第二個(gè)值是顏色值。兩者都來自數(shù)據(jù)庫。我想反過來使地理圖表動(dòng)態(tài)
查看完整描述

3 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

您正在尋找的函數(shù)是json_encode(),但如果您首先構(gòu)建輸入數(shù)組,它會(huì)更有用。


所以像下面這樣:


// Some code here to generate $arrayData from $statements, giving the following


$arrayData = [

  ['Country' => 'China', 'Color Value' => 3],

  ['Country' => 'Russia', 'Color Value' => 2.6],

  ['Country' => 'France', 'Color Value' => 2.5],

  ['Country' => 'Spain', 'Color Value' => 2.4],

  ['Country' => 'Portugal', 'Color Value' => 1.1]

];



// Then, once you have generated $arrayData


echo json_encode($arrayData);


查看完整回答
反對(duì) 回復(fù) 2023-04-28
?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

您需要先在 php 中準(zhǔn)備數(shù)組,而不僅僅是 echo json_encode($arr):


<?php


$data = [];

foreach ($something as $statements) {

   $data[] = [$statements["Country"], $statements["Color_Value"]]; 

}

$arrayData = json_encode($data);

?>

arrayData = <?= $arrayData ?>;


查看完整回答
反對(duì) 回復(fù) 2023-04-28
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

//First you need to check the structure of the result from your database query

//Then Loop through the result Object/Array to get the desired structure

//Most Common way of doing this is foreach loop 


//define an blank array 

$chart = [];


//put your axis tags/ texts in on first index

$chart[] = ['Country', 'Colour Count'];


//let's say $reult stores the data fetched from database


foreach ($result as $key => $value){

    //let's assume $value has field with country name as value 

    //and field colour_count have color count values, so

    // $value->country = Country Name

    //$value->colour_count = Color Count Value

    $chart[] = [$value->country, $value->colour_count];

}

// Once the Loop is finished, you will get an array with your desired format


//$chart = [['Country', 'Colour Count'],['France', 12],['USA',34],.....so on];


查看完整回答
反對(duì) 回復(fù) 2023-04-28
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)