FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How to convert this PHP into JavaScript
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: How to convert this PHP into JavaScript [message #173174 is a reply to message #173170] Fri, 25 March 2011 23:25 Go to previous messageGo to previous message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma:
Senior Member
On 3/25/2011 4:56 PM, Thomas 'PointedEars' Lahn wrote:
> Denis McMahon wrote:
>
>> Oltmans wrote:
>>> […] Here is the PHP
>>>
>>> array(
>>> 'abc' => '1 AND (2 OR
>>> 3)', // Define how to use
>>> the following criteria
>>> 'def' => array('marketing_campaign_id', 'status',
>>> 'status'), // Filter by these three criteria
>>> 'ghi' => array('eq', 'eq',
>>> 'eq')
>>> );
>>>
>>>
>>> I need to convert above into JavaScript object which looks like
>>>
>>> {
>>>
>>> 'abc' : '1 AND (2 OR 3)'
>>> }
>>>
>>> […]
>>
>> Here is a display of some PHP source code that generates a document which
>> contains some javascript, and passes an object to that javascript using
>> JSON.
>>
>> http://www.sined.co.uk/tmp/oltmans.htm
>>
>> And here is the output it generates:
>>
>> http://www.sined.co.uk/tmp/oltmans.php
>>
>> Looking at these will probably confuse you, but might instead be of some
>> help.
>
> Version information from the OP being missing, the correct answer is of
> course to use PHP's json_encode() function¹ to generate JSON (JavaScript
> Object Notation)² from the PHP value, which is compatible with all
> statistically significant client-side ECMAScript implementations³ (every
> syntactically valid JSON object string is a syntactically valid ECMAScript
> Object initializer/literal, and every syntactically valid JSON array string
> is a syntactically valid Array initializer/literal; the reverse is not
> true):
>
> <?php echo json_encode(array('abc' => '1 AND (2 OR 3)')); ?>
>
> ______
> ¹<http://php.net/json_encode>
> ²<http://json.org/>
> ³<http://PointedEars.de/scripts/test/es-matrix/#!>
>
>
> PointedEars


This little php program:
<?php
$arr = array('abc'=>1, 'def'=>array('marketing_campaign_id', 'status',
'status'), 'ghi'=>array('eg', 'eq', 'eq'));
$json = json_encode($arr);
print '<pre>'; print_r($arr);print'</pre>';
print 'json endoded = '.$json;
?>

generates this output:
Array
(
[abc] => 1
[def] => Array
(
[0] => marketing_campaign_id
[1] => status
[2] => status
)

[ghi] => Array
(
[0] => eg
[1] => eq
[2] => eq
)

)

json endoded =
{"abc":1,"def":["marketing_campaign_id","status","status"],"ghi":[ "eg","eq","eq"]}

--
Shelly
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: website developers requirements
Next Topic: Aste - PHP template engine
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 05:02:17 GMT 2024

Total time taken to generate the page: 0.03715 seconds