Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:16:51+00:00 2026-06-03T10:16:51+00:00

I’m having problems translating my associative array that I create in jQuery to something

  • 0

I’m having problems translating my associative array that I create in jQuery to something I can access and use in PHP.

I use the following to build an associate array:

var colors = {};
$(this).find('input[name="color_id"]').each(function() {
    var color_entry_id = $(this).val();
    colors[color_entry_id] = jQuery.makeArray($(this).siblings(".colors." + $(this).val()).find(".img_color a.selected").map(function() {
        return $(this).css("background-color");
    }));
});

Basically, the above code should return something of the form:

colors = {
    "{{ color_entry_id }}": [array of string rgb values]
}

I then send it to my PHP script with the following:

$.post(
     "test.php",
     { "edit_colors": JSON.stringify(colors) },
    ...
);

In my PHP, I want to be able to grab the array corresponding to a {{ color_entry_id }} and loop through the values to use in an update query. The following is my PHP code:

$check_colors = array(
    "rgb(204, 51, 51)" => "red",
    "rgb(102, 153, 204)" => "sky_blue",
    "rgb(0, 204, 51)" => "lime_green",
    "rgb(0, 102, 0)" => "dark_green",
    "rgb(153, 0, 0)" => "burgandy", 
    "rgb(255, 102, 51)" => "orange", 
    ...
);   

$colors = json_decode($_POST['edit_colors']);

foreach($images as $color_entry => $image_link) {
    $update_color_query = "UPDATE color SET ";
    foreach(array_keys($check_colors) as $color) {
        if(in_array($color, $colors[$color_entry])) {
            $update_color_query .= $check_colors[$color] . "=1, ";
        } else {
            $update_color_query .= $check_colors[$color] . "=0, ";
        }
    }
    $update_color_query .= "image_url='$image_link' WHERE id=$color_entry";
    mysql_query($update_color_query);
}

In the above code, images is a separate array with corresponding {{ color_entry_id }}s and image links as well. $check_colors is a PHP hardcoded associative array of rgb values to human readable colors. My problem is that the link:

in_array($color, $colors[$color_entry])

throws a “Fatal error: cannot use type stdClass as array” because $colors[$color_entry] is not becoming the two dimensional array I’m trying to get it to be. So anyone know what I am doing wrong and how I can get this two dimensional array to loop through in my PHP code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:16:52+00:00Added an answer on June 3, 2026 at 10:16 am

    The error is exactly what it says: You can’t use an OBJECT as an ARRAY. When you use json_decode, it defaults to an object. You need it to be an associative array.

    So change it to this:

    $colors = json_decode($_POST['edit_colors'], true);
    

    Now, you can iterate through $colors as you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.