0

I'm trying to create a new set of arrays from an existing array, using the code:

var stock = new Array()
    for (i = 0; i < seOutput.length; i++) {
      stock[i] = new Array(seOutput[i][0], seOutput[i][1], seOutput[i][2], seOutput[i][3], seOutput[i][4])      
}

If I console.log(seOutput); I see this:

Array
(
  [0] => Array
      (
          [0] => Blue
          [1] => Yes
          [2] => No
          [3] => Yes
          [4] => Yes
      )

  [1] => Array
      (
          [0] => Red
          [1] => No
          [2] => No
          [3] => No
          [4] => Yes
      )

This worked logically in my head, where the code seOutput[0][0] would equal Blue, but that's not what's given back in my console.log(seOutput[0][0] statement. For the statements console.log(seOutput[0]) and console.log(seOutput[0][0]) I get A (the first letter in the word Array ).

What is the proper way to access this array?

To give a bit more detail, seOutput is generated from my ajax success call to a .php page where the final bit of code is:

foreach ($json['Result']['Data']['Type'] as $i) {

  $y = "{$i['Color']},{$i['Object']},{$i['Crs']},{$i['CrsX']},{$i['CrsB']}"; 
  $x = explode("," , $y);
  array_push($output, $x);
}

then

print_r($output);

and my ajax code:

$.ajax({
              url: "seSearch.php", 
              type: "post",                  
              data: {partNumber: q , c:c},
              success: function(data) {

                  var seOutput = data;
  }
});

What's generated from that is the "array" I pasted above. Why ajax has converted this into a string I don't know....

2
  • what array are you trying to access? seOutput or stock? Commented Feb 18, 2015 at 15:25
  • stock is built from seOutput, and the for loop works, it just doesn't populates with bogus data because I'm not referencing seOutput correctly. Commented Feb 18, 2015 at 15:27

1 Answer 1

1

I think you assigned string to seOutput (containing array string representation) instead of real array in. So you access it properly but you have no array. Try print typeof(seOutput) to check it.

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

5 Comments

string - that's weird. So I don't even have an array here. Is there a simple way to convert this into an array? (Also, aside from that, was my logic on how to access this if it WERE an array correct?)
Yes, the code is correct IF it was an array. how are you building the array?
I don't know where you get this string. But there must exists original array before conversion to string. Maybe you accidentally make conversion somewhere.
you need php.net/manual/en/function.json-encode.php instead of print_r(). Also set header Content-Type: application/json to proper parsing in $.ajax
how silly of me... yeah, doing json for both solved it. Thanks @farincz

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.