0

I am building an array in JS as such:

       var slots = {}; 

                $(".taken").each(function(item) {

                    var key = $(this).attr("id");
                    slots[key] = "<?php echo $_SESSION['alias']; ?>";

                }); 

                var json = JSON.stringify(slots);
                var date = "<?php echo $_GET['date']; ?>"
             $.ajax({
                type: "POST",
                     url: "controllers/dutyupdate2.php",
                    data:{ array : json, date:  date },
                success : function(response){
                        console.log (response)
                    }//end success
                });//end ajax

In my PHP script I am posting to, I need to decode it to match the following format:

array( 'D1P'=>"JohnC" , 'D6E' => "JohnC")

I get:

Array(
    [D2E] => JohnC
    [D6E] => JohnC
    [D3BU] => JohnC
)

No matter how I decode the array, I get an indexed array with my key as the index. Am I building the array incorrectly in the JS code or decoding incorrectly?

Thanks in advance

12
  • 4
    "I get an indexed array with my key as the index" - and what exactly do you want? Commented Feb 14, 2013 at 15:16
  • 5
    I don't understand how those 2 array differ (the one you get, the one you want) Commented Feb 14, 2013 at 15:17
  • 6
    never echo php data directly into a Javascript context. one JS meta-character in that php data and you've killed the entire JS code block. ALWAYS use json_encode(), e.g. <?php echo json_encode($var) ?> so you always produce something syntactically valid. Commented Feb 14, 2013 at 15:18
  • 1
    @Jjames: The array is in the format you are looking for! print_r(array( 'D1P'=>"JohnC" , 'D6E' => "JohnC")). Commented Feb 14, 2013 at 15:59
  • 1
    Yes, I thought so from the beginning, IT was entirely my F-UP...:), I appreciate the answers not being to harsh...thanks again Commented Feb 14, 2013 at 16:34

1 Answer 1

2

This is the format you want, just displayed differently. See this PHP code to verify

$a = array( 'D1P'=>"JohnC" , 'D6E' => "JohnC");
print_r($a);

this gives

Array
(
    [D1P] => JohnC
    [D6E] => JohnC
)

as output. So, there's no need to try or search anything different.

Sign up to request clarification or add additional context in comments.

1 Comment

You are absolutely correct, I am not sure where I was FUBAR ing it up, but it is correct, I thank you for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.