1
    Array
    (
        [0] => Array
            (
                [price_id] => 1
                [website_id] => 0
                [all_groups] => 1
                [cust_group] => 32000
                [price_qty] => 2
                [price] => 90.0000

            )

        [1] => Array
            (
                [price_id] => 2
                [website_id] => 0
                [all_groups] => 1
                [cust_group] => 32000
                [price_qty] => 5
                [price] => 80.0000

            )
.......
)

the array element maybe one or two or more, if i want to pass [price_qty] and [price] value to the jquery code. how should i do? could someone make me an example. thank you

123
  • 143
  • 1
  • 9

6 Answers6

1

you should consider using JSON strings in order to use key based arrays in JavaScript.

http://php.net/json

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
GottZ
  • 4,824
  • 1
  • 36
  • 46
0

json_encode your php arrays to json

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • thank you, now, if i json_encode the array, i don't know how to only output [price_qty] and [price] value.thank you – 123 Nov 21 '12 at 14:40
0

Use json_encode to convert your php array to json :)

Matjaž
  • 81
  • 4
0

A possible solution is to convert php array structures into JSON before passing the data to the client.

Have a look at php json. And also have a look at this post.

Community
  • 1
  • 1
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Try with Json:

json_encode($array);

This will encode the array into a json object, that is friendly with javascript (and Jquery).

If you are passing it through an ajax request just echo it in the php and return that as response.

If it's in the same script you could do:

$object = json_encode($array);
echo "var myObject = $object;";

And for accessing the information in javascript/jquery you would do:

alert(myObject[0].price_id);

you use myObject[0] to access positions as in php arrays, and myObject[0].name to access what would be associative array key in an array.

For more information visit json documentation page

aleation
  • 4,796
  • 1
  • 21
  • 35
  • from your code ,i did, but can't work,json_encode($array);$object = json_encode($array); echo "var myObject = $object;"; – 123 Nov 21 '12 at 14:56
0

Well while you ask to move from [php]array to [javascript]array, this is how to. You should ue json_encode how the previous answer said, but in you javascript you can use the following code to convert a json into an array:

function json2Array = function(json){   
    var a = [];
    for(var o in json){
      a.push(json[o]);
    }   
    return a;
}

var myArray = json2Array(youPhpJsonEncode);

and you will have your array in javascript

ncubica
  • 8,169
  • 9
  • 54
  • 72
  • i am sorry, i still don't know how to do, if this is my jquery code.$('input').on('keyup', function() { var result = 0; $(' li input').each(function() { result += parseInt(this.value, 10); }); – 123 Nov 21 '12 at 14:49
  • i want to make a compare between the result and the [price_qty] value which from the php array. – 123 Nov 21 '12 at 14:50
  • Ok so what you have to do is how @aleation said in php write $object = json_encode($array); echo "var myObject = $object;"; and insert the code before any javascript code then you can iterate with jQuery Each method $.each(myObject, function(_index, _item){ var objectInArray = _item; if(objectInArray.price_qty == "somevalue"){ //do something } }); – ncubica Nov 21 '12 at 14:55
  • i did as your told.$object = json_encode($array); echo "var myObject = $object;"; there is no value alert. – 123 Nov 21 '12 at 15:09
  • before echo the value of the json_encode in you html if this has content then move to the javascript if not something went wrong in php... does this has content? – ncubica Nov 21 '12 at 15:16