0

I have an array being passed from php which looks like:
$resultsArr[123]['A']='q';
$resultsArr[123]['B']='d';
$resultsArr[113]['C']='s';
$resultsArr[113]['A']='ss';
$resultsArr[113]['B']='sd';
$resultsArr[111]['C']='sds';
$resultsArr[111]['A']='vv';
$resultsArr[111]['B']='vv';

i need to access certain values frmo this array using jquery.

i am trying to access it like
keyVal = 123; //dynamically generated
var pri = '~$results['keyVal']['B']`'

but i am getting a blank value in variable 'pri' How can this be solved?

2
  • can you tell us where you have included this code? in your parsed html or your source php file? keep in mind, jquery is client-side and smarty (php) server-side, means jquery doesn't know anything form php-code and analog Commented Aug 7, 2012 at 9:03
  • the array part is in php and has been passed on to the html using smarty. the jquery part is in the template itself Commented Aug 7, 2012 at 9:09

2 Answers 2

1

Could you not convert it to a JSON Array and then use it directly in Javascript, rather than picking out individual elements of the array?

<script>

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

</script>

Then use jQuery each to read the array.

This would give you greater flexibility in the long term of what was available to javascript for reading and manipulation.

EDIT

You can read a specific element like so, this will alert "vv":

<script>
    var myVar = myArray[111].A;
    alert(myVar);
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

can i convert this to json in jquery?
Convert it to what? You can use jQuery to iterate over the JSON array and read its values. If you know which elements you want you can do var myVar = myArray[111].A; I will do an edit for you in my answer.....
i am not getting the array in json format from php, is there an equivalent for json_encode in jquery?
you should be able to JSONify your data in Smarty: {$results|json_encode}
0

In php use :

$ResultsArr = json_encode($resultsArr);

$this->jsonResultsArr = $ResultsArr; //its seems u r using smarty.

In javascript

jsonResultsArr = "~$jsonResultsArr`";

requireValue = jsonResultsArr[111].A;

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.