0

Could not convert or access JSON object from PHP json_encode

<?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);
?>

I get only one value ie 1 in Javascript, with enabling Custom Line 1 & Custom Line 2.

And when i enabled Custom Line 3 and disable Custom Line 1 & Custom Line 2. then i get output good as in from database

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

Result 1

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

Result 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>

And i have attached an image below..

enter image description here

Now how can i access the Result 2 in an order of an Array or any other method.

And i tried using below

<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>

What i need or Need Help

Its not giving any result as 0 is a integer not string.. And if its an Array then its also tricky.. there are n number of String 0.. so how can i call one by one. or To convert into Javascript array.

Note: Result 2 is desired output, but its not in Array

2
  • do you work in clean PHP code or are you using framework or some template engine as smarty ? Commented Oct 29, 2012 at 14:34
  • 1
    Just remove the "" at var j_array = "<?php echo $j_array; ?>"; Commented Oct 29, 2012 at 14:37

4 Answers 4

2

Take a look at php manual for array_push().

Returns the new number of elements in the array.

So the correct syntax is:

array_push($f_array, $f_count);   // without `$f_array = ` at the beginning
2

Either:

array_push($f_array, $f_count);

or

$f_array[] = $f_count;

Definitely not $f_array = array_push($f_array, $f_count).

Next, no quotes around the JSON array, since that becomes a Javascript string:

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

Next, Javascript array elements are accessed as such:

j_array[0]

Not j_array.0.

2
  • Why i cant use array_push, if used it return only one single value.. And Good explaination. Commented Oct 29, 2012 at 14:57
  • Please RTFM entry for array_push. It returns the new number of elements in the array (i.e. a number), not the new array. The array is modified in-place via references, you do not need the return value. You're just overwriting your just-pushed-into array with a number. Commented Oct 29, 2012 at 15:00
1
var j_array = <?php echo $j_array; ?>;

Without " "

When you put the brackets you escape the json_encoded array at your javascript..

3
  • Not an answer to the question, but true nonetheless! Commented Oct 29, 2012 at 14:43
  • where is that $interval comming from at your loop ? Commented Oct 29, 2012 at 14:46
  • $interval is defined. and its working.. good.. Commented Oct 29, 2012 at 14:55
1

Look at the function signature of array_push().

It does not return the extended array, but rather uses the first argument as a variable reference and returns an integer. On Custom Line 2, you are assigning the return value of array_push() to you array, this is why you're always getting 1 as a result.

Remove the assignation on Custom Line 2, like shown below, and you'll get what you expect:

array_push($f_array, $f_count);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.