0

PHP json_encode から JSON オブジェクトを変換またはアクセスできませんでした

<?php
$f_array = array();   // ---- Custom Line 1
for ($i=0; $i < $interval; ++$i) 
{
    $t_date = date('Y-m-d', strtotime($k_date1 . "+ $i day"));    

    $f_array = array();
    $f_query = mysql_query("select COUNT(j.job_id) as `job_count`
                            from jobs j
                            where j.job_posted_date 
                            LIKE '%$t_date%' and j.job_status = 3");

    if (mysql_num_rows($f_query) > 0) 
    {
        $f_query_data = mysql_fetch_array($f_query);
        $f_count = $f_query_data['job_count'];

        $f_array = array_push($f_array, $f_count);   // ---- Custom Line 2
        // $f_array[] = $f_count;   // ---- Custom Line 3
    }
}

$j_array = json_encode($f_array);
?>

&1を使用して、Javascriptで1つの値のみを取得します。enabling Custom Line 1Custom Line 2

そして、 iと&enabled Custom Line 3を無効にすると。次に、データベースからのように出力が得られますCustom Line 1Custom Line 2

<script>
var j_array = "<?php echo $j_array; ?>";
</script>

結果1

<script>
var j_array = "1";
</script>

結果2

<script>
var j_array = "["0","0","0","2","0","0","0","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","2","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","25","0","0","0","0","0","7","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1","0","0","1","1","47","0","1","1","0","0","0","0","0","0","3","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]";
</script>

そして、私は下の画像を添付しました..

ここに画像の説明を入力

次に、または他のメソッドResult 2の順序でアクセスするにはどうすればよいですか。Array

そして、私は以下を使ってみました

<script>
    alert(j_array);
</script>

<script>
    alert(j_array.0); // as an Object mightbe .. or I dont have any idea on it, so experimenting ..
</script>

必要なものまたは必要なヘルプ

ないので結果0が得られintegerませんstring..そして、その場合Arrayもトリッキーです..nいくつかあります..ではString 0、どうすれば1つずつ呼び出すことができますか。または Javascript 配列に変換します。

注:Result 2望ましい出力ですが、そうではありませんArray

4

4 に答える 4

2

また:

array_push($f_array, $f_count);

また

$f_array[] = $f_count;

絶対に違います $f_array = array_push($f_array, $f_count)

次に、JSON 配列を引用符で囲みません。これは Javascript 文字列になるためです。

var j_array = <?php echo $j_array; ?>;

次に、Javascript 配列要素に次のようにアクセスします。

j_array[0]

違い j_array.0ます。

于 2012-10-29T14:40:36.783 に答える
2

のphpマニュアルを見てくださいarray_push()

配列内の新しい要素数を返します。

したがって、正しい構文は次のとおりです。

array_push($f_array, $f_count);   // without `$f_array = ` at the beginning
于 2012-10-29T14:38:28.820 に答える
1

の関数シグネチャを見てくださいarray_push()

拡張配列を返すのではなく、最初の引数を変数参照として使用し、整数を返します。Custom Line 2では、戻り値array_push()を配列に割り当てています。これが、常に1結果として得られる理由です。

以下に示すように、Custom Line 2 の割り当てを削除すると、期待どおりの結果が得られます。

array_push($f_array, $f_count);
于 2012-10-29T14:37:47.543 に答える
1
var j_array = <?php echo $j_array; ?>;

それなし " "

括弧を入れると、javascript で json_encoded 配列をエスケープします。

于 2012-10-29T14:38:07.120 に答える