如何将PHP数组转换成这样的格式
Array
(
[0] => 001-1234567
[1] => 1234567
[2] => 12345678
[3] => 12345678
[4] => 12345678
[5] => AP1W3242
[6] => AP7X1234
[7] => AS1234
[8] => MH9Z2324
[9] => MX1234
[10] => TN1A3242
[11] => ZZ1234
)
到下面格式的Javascript数组?
var cities = [
"Aberdeen",
"Ada",
"Adamsville",
"Addyston",
"Adelphi",
"Adena",
"Adrian",
"Akron",
"Albany"
];
如果您需要将多维PHP数组直接打印到html页面源代码中,并以一种缩进的人类可读的Javascript对象符号(又名JSON)方式,请使用我在http://php.net/manual/en/function.json-encode.php#102091中找到的这个由绰号为“bohwaz”的人编写的漂亮函数。
<?php
function json_readable_encode($in, $indent = 0, $from_array = false)
{
$_myself = __FUNCTION__;
$_escape = function ($str)
{
return preg_replace("!([\b\t\n\r\f\"\\'])!", "\\\\\\1", $str);
};
$out = '';
foreach ($in as $key=>$value)
{
$out .= str_repeat("\t", $indent + 1);
$out .= "\"".$_escape((string)$key)."\": ";
if (is_object($value) || is_array($value))
{
$out .= "\n";
$out .= $_myself($value, $indent + 1);
}
elseif (is_bool($value))
{
$out .= $value ? 'true' : 'false';
}
elseif (is_null($value))
{
$out .= 'null';
}
elseif (is_string($value))
{
$out .= "\"" . $_escape($value) ."\"";
}
else
{
$out .= $value;
}
$out .= ",\n";
}
if (!empty($out))
{
$out = substr($out, 0, -2);
}
$out = str_repeat("\t", $indent) . "{\n" . $out;
$out .= "\n" . str_repeat("\t", $indent) . "}";
return $out;
}
?>
这是我的函数。JavaScript必须在PHP下,否则使用SESSION。
<?php
$phpArray=array(1,2,3,4,5,6);
?>
<div id="arrayCon" style="width:300px;"></div>
<script type="text/javascript">
var jsArray = new Array();
<?php
$numArray=count($phpArray);
for($i=0;$i<$numArray;$i++){
echo "jsArray[$i] = ". $phpArray[$i] . ";\n";
}
?>
$("#arrayCon").text(jsArray[1]);
</script>
最后一行可以是....text(jsArray);会显示"1,2,3,4,5,6"
这里有许多好的和复杂的解决方案,这是一种不需要在用户端解析json的方法。
$mtc=array();
$replace=array();
$x = json_encode($thearray);
preg_match_all('/"[a-z0-9_]*":/',$x,$mtc);
foreach($mtc[0] as $v){
array_push($replace,str_replace('"','',$v));
}
$x = str_replace($mtc[0],$replace,$x);
echo '<script type="text/javascript">x='.$x.';console.log(x);</script>';
这适用于具有多个级别的索引数组和关联数组(任何组合),并且不需要用户端json解析。
我也遇到了同样的问题,我就是这样做的。
/*PHP FILE*/
<?php
$data = file_get_contents('http://yourrssdomain.com/rss');
$data = simplexml_load_string($data);
$articles = array();
foreach($data->channel->item as $item){
$articles[] = array(
'title' => (string)$item->title,
'description' => (string)$item ->description,
'link' => (string)$item ->link,
'guid' => (string)$item ->guid,
'pubdate' => (string)$item ->pubDate,
'category' => (string)$item ->category,
);
}
// IF YOU PRINT_R THE ARTICLES ARRAY YOU WILL GET THE SAME KIND OF ARRAY THAT YOU ARE GETTING SO I CREATE AN OUTPUT STING AND WITH A FOR LOOP I ADD SOME CHARACTERS TO SPLIT LATER WITH JAVASCRIPT
$output="";
for($i = 0; $i < sizeof($articles); $i++){
//# Items
//| Attributes
if($i != 0) $output.="#"; /// IF NOT THE FIRST
// IF NOT THE FIRST ITEM ADD '#' TO SEPARATE EACH ITEM AND THEN '|' TO SEPARATE EACH ATTRIBUTE OF THE ITEM
$output.=$articles[$i]['title']."|";
$output.=$articles[$i]['description']."|";
$output.=$articles[$i]['link']."|";
$output.=$articles[$i]['guid']."|";
$output.=$articles[$i]['pubdate']."|";
$output.=$articles[$i]['category'];
}
echo $output;
?>
/* php file */
/*AJAX COMUNICATION*/
$(document).ready(function(e) {
/*AJAX COMUNICATION*/
var prodlist= [];
var attrlist= [];
$.ajax({
type: "get",
url: "php/fromupnorthrss.php",
data: {feeding: "feedstest"},
}).done(function(data) {
prodlist= data.split('#');
for(var i = 0; i < prodlist.length; i++){
attrlist= prodlist[i].split('|');
alert(attrlist[0]); /// NOW I CAN REACH EACH ELEMENT HOW I WANT TO.
}
});
});
我希望这能有所帮助。