0

Good day,

I have this data which I got from my query on PHP its is currently a JSON Format, now I want to convert it into array so I can use it on my pdf. How can I do this?

so in order for me to use it on my javascript I used

var inventory = <?php echo json_encode($inventory); ?> ;

my JSON data :

var inv = [
{"xid":96,"xitem":"CARBOCISTEINE 500MG CAP (MYREX) BOX",
"itemId":852,
"price":3,
"mprice":3
 },
{"xid":253,"xitem":"CIPROFLOXACIN 500MG TAB (PROSELOC\/FLAMINGO)",
 "itemId":1165,
 "price":0,
 "mprice":0
 }];

I tried

var rows = <?php echo json_encode($inventory); ?> ;
var arr = $.map(rows, function(el) { return el; });

and

when I console.log(arr); I still get the object structure not the array structure that I wanted.

I also tried

var result = [];

for(var i in rows)
    result.push([i, rows [i]]);

console.log(result);

but it gives me

[ ["0",Object { xid=96,xitem="CARBOCISTEINE 500MG CAP (MYREX) BOX",itemId=852,price=3,mprice=3}],
["1",Object{etc..}]];

instead

I want it to have a structure like

[96,"CARBOCISTEINE 500MG CAP (MYREX) BOX",852,3,3], [253,"CIPROFLOXACIN 500MG TAB (PROSELOC\/FLAMINGO)",1165,0,0]

Is there something I am missing on my code or How should I be able to do this? thanks..

3
  • 1
    when you json_decode in php, it already creates an array, so would be just easier to create what you want to do in php instead of javascript Commented Sep 19, 2015 at 10:43
  • 1
    you should search for "how to convert javascript object to array" Commented Sep 19, 2015 at 10:43
  • Thanks for the ideas guys.. my keyword is wrong all this time xD Commented Sep 19, 2015 at 10:44

3 Answers 3

1

You can use this:

var arr = inv.map(function (obj) {  return [obj.xid, obj.xitem, obj.itemId, obj.price, obj.mprice]})
console.log(arr);

If you do small changes to your map callback function, then it will be fine.

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

1 Comment

This is what Im Trying to achieve.. Thanks sir @Beroza Paul
0

you can do just

class MyClass
{
    public $var1 = 'value 1';
    public $var2 = 'value 2';
    public $var3 = 'value 3';
}

$class = new MyClass();
$arr = [];

foreach($class as $key => $value) {
    $arr[] = $value;
}

echo json_encode($arr); //here's to check

in PHP - you can make a collection of object like this and iterate first oveer this collection to get result as you wanted to have

1 Comment

Thanks for the answer mr @m.antkowicz but Im already using my php's json structure for my view, and size would be doubled if I pass another Array structure on my view for my script.. thats why I just wanted to convert the existing variable with object structure into another variable with array structure in it.. hope i make sense.. thanks for the effort too..
0

You shouldn't have to hard code the keys. The following works:

var arrayFromObject = inv.map(function (item) {
    var arr = [];
    for (var p in item) arr.push(item[p])
    return arr;
});

Comments

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.