0

I have a javascript dynimic array creator code as below

function createArray() {

    var myArr = new Object();
    myArr[0] = createSubArray('apple');
    myArr[1] = createSubArray('Mango');
    myArr[2] = createSubArray('Pineapple');
    myArr[3] = createSubArray('Grape');
    document.getElementById("a").innerHTML = JSON.stringify(myArr);
    return myArr;

}


function createSubArray(name){
    var arr = new Object();
    elems = document.getElementsByName(name);
    for (var i=0;i<elems.length;i++){
        if (elems[i].checked){
            arr[name] =  elems[i].value;
            arr['price'] =  elems[i].getAttribute('data-price');
        }
    }
    return arr;
   }

In this code i need to send the value as an array not an object. I have tried changing the new Object(); to new Array(); in createArray function but when i json_decode i get the result as below

array
  0 => 
    object(stdClass)[1]
      public 'apple' => string 'light' (length=5)
      public 'price' => string '10' (length=2)
  1 => 
    object(stdClass)[2]
      public 'Mango' => string 'light' (length=5)
      public 'price' => string '30' (length=2)
  2 => 
    object(stdClass)[3]
      public 'Pineapple' => string 'light' (length=5)
      public 'price' => string '50' (length=2)
  3 => 
    object(stdClass)[4]
      public 'Grape' => string 'dark' (length=4)
      public 'price' => string '80' (length=2)

if i change new Object in createSubArray function i din't get any values.. Kindly check below

new Object changed to new Array in createArray function

new Object changed to new Array in createArray And createSubArray function

How i can change the object into an array ?

Kindly help me in solving this

Varun Sridharan
  • 1,983
  • 2
  • 20
  • 54

1 Answers1

3
$array = json_decode($json, true);

Setting the second parameter to true forces json_decode to return an associative array instead of a stdClass.

yannis
  • 6,215
  • 5
  • 39
  • 49
  • i need to change this createArray function to dynimic – Varun Sridharan Sep 18 '12 at 04:38
  • you can see there is a line like this `myArr[0] = createSubArray('apple');` i need to change that line into dynimc because i may have 10 groups of radio button those radio button is dynimicaly created so i need to change it a dynimic – Varun Sridharan Sep 18 '12 at 04:41
  • 1
    @vaahost Well the first thing you'll have to do is get all your radio buttons, look at [`getElementsByTag`](https://developer.mozilla.org/en-US/docs/DOM/element.getElementsByTagName) for an easy way to do that. Then you just need to iterate through the list of radio buttons, extract their name and add it to your list. – yannis Sep 18 '12 at 04:44
  • I am very new to javascript can you show me an example sir pls – Varun Sridharan Sep 18 '12 at 04:50
  • @vaahost You already [asked this](http://stackoverflow.com/questions/12456432/radio-button-javascript-array) for your `createSubArray`, and you got a [good answer](http://stackoverflow.com/a/12457065/99456). You just need to do the same for your `createArray` function, the only difference being that you'll need to fill your `elems` array with the radio buttons – yannis Sep 18 '12 at 04:55
  • Hmm Any way i would try but if possible can you give me an example .? – Varun Sridharan Sep 18 '12 at 05:00
  • @vaahost Here's a sub optimal solution: `function createArray() { var arr = new Array(); elems = document.getElementsByTagName("input"); l = 0; for (var i=0; i – yannis Sep 18 '12 at 05:12
  • Sorry its not working any way i am trying and searching for it – Varun Sridharan Sep 18 '12 at 05:17
  • @vaahost Yes, of course it's not working ;) I can't guess what your HTML is like, I've given you a general solution, you'll need to adapt it to your own code. – yannis Sep 18 '12 at 05:19