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

6 Answers 6

1

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

http://php.net/json

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

Comments

0

json_encode your php arrays to json

1 Comment

thank you, now, if i json_encode the array, i don't know how to only output [price_qty] and [price] value.thank you
0

Use json_encode to convert your php array to json :)

Comments

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.

Comments

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

1 Comment

from your code ,i did, but can't work,json_encode($array);$object = json_encode($array); echo "var myObject = $object;";<script> alert(myObject[0].price_id); </script>
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

5 Comments

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); });
i want to make a compare between the result and the [price_qty] value which from the php array.
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 } });
i did as your told.$object = json_encode($array); echo "var myObject = $object;";<script> jQuery(document).ready(function () { jQuery.each(myObject, function(_index, _item){ var objectInArray = _item; if(objectInArray.price_qty == 2){ alert('hello'); } }); )} </script> there is no value alert.
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?

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.