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

  • Home
  • SEARCH
  • 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 7008481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:44:26+00:00 2026-05-27T21:44:26+00:00

I am passing an array from jQuery to php. The array is generated from

  • 0

I am passing an array from jQuery to php.

The array is generated from a table with this code:

    var stitchChartArray = [];
    var row = 0;

    // turn stitch chart into array for php
    $('#stitchChart').find('tr').each(function (index, obj) {

        //first row is table head- "Block #"
        if(index != 0){
            stitchChartArray.push([]);
            var TDs = $(this).children();
            $.each(TDs, function (i, o) {
                var cellData = [$(this).css('background-color'), $(this).find("img").attr('src')];
                stitchChartArray[row].push(cellData);
            });
            row++;
        }

    });

In console it looks like this:

[[["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 75, 60)", "symbols/184.png"], ["rgb(75, 90, 60)", "symbols/177.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 75, 60)", "symbols/184.png"], 7 more...], [["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(75, 90, 60)", "symbols/177.png"], ["rgb(98, 119, 57)", "symbols/210.png"], 7 more...], [["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(105, 105, 105)", "symbols/163.png"], ["rgb(150, 150, 195)", "symbols/72.png"], 7 more...], [["rgb(75, 165, 105)", "symbols/187.png"], ["rgb(134, 158, 134)", "symbols/64.png"], ["rgb(165, 180, 180)", "symbols/171.png"], 7 more...], [["rgb(60, 150, 75)", "symbols/189.png"], ["rgb(120, 120, 90)", "symbols/225.png"], ["rgb(143, 163, 89)", "symbols/209.png"], 7 more...]]

It represents each row of a table->each cell of row->[0]rgb value of bg of cell [1]icon in cell.

This jQuery code returns the correct element(and rgb value) from the array:

alert(stitchChartArray[1][1][0]); //row 1,cell 1, first value(rgb)

But when it gets sent to the php script with this:

$.post('makeChartPackage.php', {'stitchChart[]': stitchChartArray }, function(data){
        alert(data);
    });

The php throws an error:

Cannot use string offset as an array in /Users/tnt/Sites/cross_stitch/makeChartPackage.php on line 33

$stitchChart = $_POST['stitchChart']; 
echo $stitchChart[1][1][0]; //line 33

I am assuming I am either constructing the array incorrectly or passing it to the php script incorrectly.

EDIT:
I did this to return the array to jQuery:

$stitchChart = $_POST['stitchChart'];
print_r($stitchChart); 

And here was the result:
Array
(
[0] => rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(180, 195, 105),symbols/388.png,rgb(165, 165, 120),symbols/235.png,rgb(75, 75, 60),symbols/184.png,rgb(90, 90, 45),symbols/195.png,rgb(120, 120, 75),symbols/156.png,rgb(105, 105, 105),symbols/163.png
[1] => rgb(105, 105, 105),symbols/163.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(165, 165, 120),symbols/235.png,rgb(120, 120, 75),symbols/156.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 90, 60),symbols/177.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png
[2] => rgb(105, 105, 105),symbols/163.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(75, 90, 60),symbols/177.png,rgb(98, 119, 57),symbols/210.png,rgb(75, 90, 60),symbols/177.png,rgb(75, 75, 60),symbols/184.png,rgb(105, 105, 105),symbols/163.png,rgb(120, 120, 90),symbols/225.png,rgb(105, 105, 105),symbols/163.png

It appears the array is not multidimensional?

  • 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-05-27T21:44:27+00:00Added an answer on May 27, 2026 at 9:44 pm

    $_POST['stitchChart'] in the context you have addressed it there is (effectively) a JSON representation of a multidimensional array, stored as a string. When you treat a string as a multidimensional indexed array in PHP, you will get that error. The first [x] is treated as a “string offset” – i.e. the character at position x – but the next and any subsequent [x] addresses can only be treated as arrays (you cannot get a substring of a single character) and will emit the error you have received.

    To access your data as an array in PHP, you need to use json_decode():

    $stitchChart = json_decode($_POST['stitchChart'],TRUE); 
    echo $stitchChart[1][1][0];
    

    EDIT

    Because the jQuery data argument seemingly can’t deal with multidimensional arrays, you should use Douglas Crockford’s JSON-js library and pass the result into data as a string. NB: use json2.js.

    Here is how you could do this:

    stitchChartArray = JSON.stringify(stitchChartArray);
    $.post('makeChartPackage.php', {'stitchChart': stitchChartArray }, function(data){
        alert(data);
    });
    

    If you use this code, my original PHP suggestion should work as expected.

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

Sidebar

Related Questions

I'm passing from the controller an array generated by the next code: public ActionResult
My issue is passing a PHP variable from my custom module to jQuery. I
Possible Duplicate: Passing JavaScript Array To PHP Through JQuery $.ajax I'm trying to pass
I have an ArrayCollection of objects. I'm passing this array to a horizontallist as
I am passing a PHP array to a javascript function. The only way I
I'm using the JQuery autocomplete plug in, and am passing in an array of
I'd like to pass content back and forth from PHP to jQuery and vice
I'm passing an array from form inputs (checkboxes) using the get method. When doing
I am passing in a jQuery object into a function from another file via
I'm passing an array from my Rails app to Javascript via a js.erb template.

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.