Jump to content

help with json_encode


acidpunk

Recommended Posts

Hey everyone. So i'm trying to json_encode one of my arrays like such:

 

heres an example of how the array is being created (From the array results, everything seems to be fitting OK into the actual array creation) but it's just the actual array creating a multi demension after each result.

 

$num = 1;
$num2 = 1;
$i = 0;
if ($num > 0)
{
    $array[$i][1] = 40;
}
if ($num2 > 0)
{
    $array[$i][2] = 50;
}
++$i;

 

 

I'm trying to achive this:

var array_one_results = new Array();
 
array_one[1][0] = 40;
array_one[1][0] = 50;
 
array_one[2][1] = 40;
array_one[2][1] = 50;

right now, this is my results:

var array_name = new Array();
 
array_name["0"] = new Array();array_taken["0"]["0"] = "10";

array_taken["0"]["1"] = "20";

 
array_name["1"] = new Array();array_taken["1"]["0"] = "10";

array_taken["1"]["1"] = "20";

 
array_name["2"] = new Array();array_taken["2"]["0"] = "10";

array_taken["2"]["1"] = "20";

 
array_name["3"] = new Array();array_taken["3"]["0"] = "10";

array_taken["3"]["1"] = "20";

 
array_name["4"] = new Array();array_taken["4"]["0"] = "10";

array_taken["4"]["1"] = "20";

It's creating a new array for each. If anyone could help me through this, i'd really appreciate it.

 

 

I'm encoding it with this:

function js_str($s)

{

   return '"'.addcslashes($s, "\0..\37\"\\").'"';

}



function js_array($array, $keys_array)

{

    foreach ($array as $key => $value)

    {

        $new_keys_array = $keys_array;

        $new_keys_array[] = $key;

        if(is_array($value))

        {          

          echo 'array_name';

          foreach($new_keys_array as $key)

          {

              echo '["'.$key.'"]';

          }

          echo ' = new Array();';



          js_array($value, $new_keys_array);

        }

        else

        {

            echo 'array_taken';

             foreach($new_keys_array as $key)

             {

                echo '["'.$key.'"]';

              }

              echo ' = '.js_str3($value).";\n";                        

        }

    }

}
Link to comment
Share on other sites

I'm trying to achive this:

var array_one_results = new Array();
 
array_one[1][0] = 40;
array_one[1][0] = 50;
 
array_one[2][1] = 40;
array_one[2][1] = 50;

 

I assume you mean

var array_one_results = new Array();
 
array_one[1][0] = 40;
array_one[1][1] = 50;
 
array_one[2][0] = 40;
array_one[2][1] = 50;
As long as you can get your PHP array to look exactly like the Javascript "array" you want,

var array_name = <?=json_encode($array)?>;
That's all it takes. Edited by requinix
Link to comment
Share on other sites

What is this weird JavaScript code generator stuff, anyway? I mean, what are you trying to achieve in the end?

 

Generating code is generally a very bad idea, and it's particularly bad in a web context. You waste your time struggling with syntax noise, you'll probably screw up some detail in the highly complex JavaScript grammar, and you invite the whole world to try a code injection attack. For example, if you plan to put the generated code into a script element, you've already lost, because you've failed to escape critical characters like the backslash or the ampersand which can be used for a cross-site scripting attack.

 

Long story short: You're almost certainly doing it wrong. If you want to exchange data, you should do exactly that. Just make a script which JSON-encodes the array with the built-in json_encode() function and echos the result. You can then fetch the data with Ajax.

Link to comment
Share on other sites

But thank you for the response, I actually originally did do exactly what you said, but it did not work.

If you say that because you got object notation {...} then that doesn't matter much: objects and arrays in Javascript are very nearly interchangeable. (That's why I said "array" with quotation marks.)

 

How does it "not work"? Or put another way, what output are you getting and what are you trying to get? Because like Jacques1 said json_encode() is what you should be using, so any problems you're having will about how you're constructing the PHP array or using the Javascript array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.