How to write the syntax for formatting such a json string? () For example, there is a two-dimensional array.
$ Myarrnews = array ();
$ Myarrnews = explode ("| ** |", $ mystr); // the content comes from $ mystr, including multiple news Records.
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]); // $ myarrline is one of the records, including multiple fields
Print_r ($ myarrline); // A set of fields for each news, for example, Array ([0] => 10 [1] => 2166 [2] => innovation...
Now we need a json string in the format of: (set to display 5 entries per page)
{
"Ret": 0,
"Data ":{
"Newslist ":[
{
"Title": "1st News Headlines ",
"Commentcount": 0,
"Source": "source ",
"Nid": 10,
"Digest": "News Introduction ",
"Ptime": "14:11:43"
},
{
"Title": "2nd News Headlines ",
"Commentcount": 0,
"Source": "source ",
"Nid": 9,
"Digest": "News Introduction ",
"Ptime": "13:12:56"
},
{
"Title": "3rd News Headlines ",
"Commentcount": 0,
"Source": "source ",
"Nid": 8,
"Digest": "News Introduction ",
"Ptime": "12:21:11"
},
{
"Title": "4th News Headlines ",
"Commentcount": 0,
"Source": "source ",
"Nid": 7,
"Digest": "News Introduction ",
"Ptime": "11:21:00"
},
{
"Title": "5th News Headlines ",
"Commentcount": 1,
"Source": "source ",
"Nid": 5,
"Digest": "News Introduction ",
"Ptime": "19:20:01"
}
],
"Totalnum": 5
},
"Msg": "OK"
}
//----------------------------
How should I write the php syntax? Please help. please try again!
Reply to discussion (solution)
I am a php Cainiao. I am definitely a dish. please reply to The Times code and steps for details! Thank you.
$ What is mystr? you always need to post the raw data.
[Code = php] Further details:
$ Weblink = "http://www.xxx.net.cn/news/getnews.asp? Cid = 4 & topnum = 10 ";
$ Mystr = file_get_contents ($ weblink );
$ Myarrnews = array ();
// Split the News condition into an array
$ Myarrnews = explode ("| ** |", $ mystr );
// Print_r ($ myarrnews); // The set of the first 10 data entries under the category ID
// Split specific content conditions into arrays
/*
Next, we will describe the collected data set. that is to say, each piece of data includes the following field information, and this information has been obtained in $ myarrnews!
0: indicates the number of collected information.
1: Original information ID (News Category ID)
2: information title
3: Information category
4: information content
5: whether the homepage image is used
6. traffic volume
7: Released
8: Subtitle
9: information release time
*/
$ Obj-> ret = 0;
$ Newslist = array ("newslist" => array ());
For ($ I = 0; $ I
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]);
Print_r ($ myarrline); // A set of fields in each news, for example: array ([0] => 10 [1] => 2166 [2] => innovative trade union work creates a family of model employees [3] => School News [4] => .. ..
For example:
If ($ myarrline [$ I]! = ""){
$ Newslist [$ I] => 'title' = $ myarrline [2];
$ Newslist [$ I] => commentcount = 0;
$ Newslist [$ I] => source = $ myarrline [7];
$ Newslist [$ I] => nid = $ myarrline [1];
$ Newslist [$ I] => replycount = 0;
$ Newslist [$ I] => ptime = $ myarrline [9];
}
The following format will not be written (or the above is incorrect). please help me to add it.
Please provide a real url!
I don't know how to discuss the original information?
Sorry, for security reasons, the original address cannot be provided!
The information has been provided in detail! I don't know what other information Daxia wants?
The generated json string can be similar.
You have to give at least one piece of data (even fictitious)
And the correspondence with json members
In fact, if you read the code on the third floor, you can see this line:
$ Newslist [$ I] => 'title' = $ myarrline [2];
The expected format can be roughly guessed, and I only need the approximate syntax format and do not need to correspond to each field one by one (in fact, some fields still need to be transformed !). Now that you want to see the detailed information, I will explain it again.
// Configure //---------------------------------------------------------------------------------------------------
$ Weblink = "http://www.xxx.net.cn/news/getnews.asp? Cid = 4 & topnum = 5 ";
$ Mystr = file_get_contents ($ weblink );
$ Myarrnews = array ();
// Split the News condition into an array
$ Myarrnews = explode ("| ** |", $ mystr );
// Print_r ($ myarrnews); // set of the first five data entries under the category ID
// Split specific content conditions into arrays
// The above code can obtain a total of 5 records of news (such as College News) classified by cid = 4 from asp web pages
// Configure //--------------------------------------------------------------------------------------------------
// In the following code, the variable $ myarrline is a News Record. The news record contains 10 fields and the value has been obtained. Among them, the first field is the news ID (nid), [1] => 2nd, (refer to the code of building 0 ). Among them, the first field is the news title, for example: [2] => innovative trade union work creates a family of model employees, and so on. I will not explain it one by one. This news is the first of the 5 News records obtained. Do you understand !?
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]);
Print_r ($ myarrline); // A set of fields in each news, for example: array ([0] => 5 [1] => 2166 [2] => innovative trade union work creates a family of model employees [3] => School News [4] => .. ..
//--------------------------------------------------------------------
// The final json string that needs to be converted to form the 0 # floor. newslist is an array that contains five records, and each record must contain a title... . (In fact, you don't have to consider the $ myarrline [I] value corresponding to each field. you can see the 3 # floor code $ newslist [$ I] => 'title' = $ myarrline [2]; it's all clear! Even if you only write one title, I just do it with a cat and a tiger! OK ?)
Exhausted me!
$newslist[$i] = array( 'title' => $myarrline[2], 'commentcount' => 0, 'source' => $myarrline[7], 'nid' => $myarrline[1], 'replycount' => 0, 'ptime' => $myarrline[9],);
Helping people make trouble
First of all, I would like to thank xuzuning for his reply. However, I am not sure about this code. what I need is the complete code of the json string on the 0 # floor, five records in the newlist array are implemented cyclically.
Thanks again!
$ar = arrat( ' ret' => 0, 'data' => array( 'newslist' => $newslist, ),);echo json_encode($ar);
$ Newslist Generation #8 has been used as an example. you can refer to it by yourself.
In fact, I wanted:
//----------------------------------------
$ Obj-> ret = 0;
$ Newlist = array ();
$ Data = array ("newslist" => $ newlist, "totalnum" => 5 );
For ($ I = 0; $ I
Echo "-----------------";
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]);
Print_r ($ myarrline); // A set of fields in each news, for example: array ([0] => 10 [1] => 2166 [2] => innovative trade union work creates a family of model employees [3] => School News [4] =>
If ($ myarrnews [$ I]! = ""){
// Echo "No $ I ";
$ Newsobj = new News ();
$ Newsobj-> setTitle ($ myarrline [2]);
// Echo "========>". $ newsobj-> getTitle ();
$ Newsobj-> setCommentcount (0 );
$ Newsobj-> setSource ($ myarrline [7]);
$ Newsobj-> setNid ($ myarrline [1]);
$ Newsobj-> setReplycount (0 );
$ Newsobj-> setPtime ($ myarrline [9]);
$ Newslist [$ I] =>$ newsobj; // The value assignment is incorrect.
}
Else
{
Echo "no data is collected in the current year! Maybe this site does not have the latest data! ";
Return;
}
}
$ Obj-> data = $ data;
/* Echo "total:". $ myarrline [0]."
";
Echo "original ID:". $ myarrline [1]."
";
Echo "title:". $ myarrline [2]."
";
Echo "Category:". $ myarrline [3]."
";
// $ Bodyneirong = preg_replace ('<\ s * img ([^>] *?)> ', '', Myarrline [4]);
Echo "content:". $ myarrline [4]."
";
Echo "image:". $ myarrline [5]."
";
Echo "access volume:". $ myarrline [6]."
";
Echo "publisher:". $ myarrline [7]."
";
Echo "subtitle:". $ myarrline [8]."
";
Echo "release date:". $ myarrline [9]."
";
Echo"
";
*/
$ Obj-> msg = "OK ";
Print_r (json_encode ($ obj ));
$ Newslist = NULL;
$ Obj = NULL;
?>
//-----------------------------------
$ Newslist [$ I] =>$ newsobj; // but there is a problem with the value assignment here !!
In addition, although I only learned php for 2 hours in total, I also know
$ Obj-> ret = 0;
And
$ Obj = array (
There is a difference!
$ Data = array ("newslist" => $ newlist, "totalnum" => 5 );
Obviously, the values should be placed after the for loop instead of the previous values.
No, I am still prompted that there is a problem, and the mobile code is not:
$ Obj-> ret = 0;
$ Newslist = array ();
$ Data = array ("newslist" => $ newslist, "totalnum" => 5 );
For ($ I = 0; $ I
Echo "-----------------";
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]);
Print_r ($ myarrline); // A set of fields in each news, for example: array ([0] => 10 [1] => 2166 [2] => innovative trade union work creates a family of model employees [3] => School News [4] =>
If ($ myarrnews [$ I]! = ""){
Echo "No $ I ";
$ Newsobj = new News ();
$ Newsobj-> setTitle ($ myarrline [2]);
Echo "========>". $ newsobj-> getTitle (); // you can obtain the title value here
$ Newsobj-> setCommentcount (0 );
$ Newsobj-> setSource ($ myarrline [7]);
$ Newsobj-> setNid ($ myarrline [1]);
$ Newsobj-> setReplycount (0 );
$ Newsobj-> setPtime ($ myarrline [9]);
$ Newslist [$ I] =>$ newsobj; // if the value assignment is incorrect, the system prompts Parse error: parse error in.
}
Else
{
Echo "no data is collected in the current year! Maybe this site does not have the latest data! ";
Return;
}
}
$ Obj-> data = $ data;
/* Echo "total:". $ myarrline [0]."
";
Echo "original ID:". $ myarrline [1]."
";
Echo "title:". $ myarrline [2]."
";
Echo "Category:". $ myarrline [3]."
";
// $ Bodyneirong = preg_replace ('<\ s * img ([^>] *?)> ', '', Myarrline [4]);
Echo "content:". $ myarrline [4]."
";
Echo "image:". $ myarrline [5]."
";
Echo "access volume:". $ myarrline [6]."
";
Echo "publisher:". $ myarrline [7]."
";
Echo "subtitle:". $ myarrline [8]."
";
Echo "release date:". $ myarrline [9]."
";
Echo"
";
*/
$ Obj-> msg = "OK ";
Print_r (json_encode ($ obj ));
For ($ I = 0; $ I
Echo "-----------------";
$ Myarrline = array ();
$ Myarrline = explode ("| |%%| |", $ myarrnews [$ I]);
Print_r ($ myarrline); // A set of fields in each news, for example: array ([0] => 10 [1] => 2166 [2] => innovative trade union work creates a family of model employees [3] => School News [4] =>
If ($ myarrnews [$ I]! = ""){
$ Newslistarr = array ('title' => $ myarrline [2], 'commentcount' => 0, 'source' => $ myarrline [7], 'nid' => $ myarrline [1], 'replace count' => 0, 'ptime' => $ myarrline [9],);
$ Newslist [$ I] = $ newslistarr;
}
//--------------------------------------------
This will not work either. after formatting, the result is: {"ret": 0, "data": {"newslist": [], "totalnum": 5}, "msg ": "OK"} the array is empty.
But how can I write the php syntax?
$ Newslist [$ I] = $ newsobj
You have more>
$ Newslist [$ I] = $ newsobj
You have more>
Yes, I wrote an error here, but after correction, it still won't work.
Formatted result: {"ret": 0, "data": {"newslist": [], "totalnum": 5}, "msg ": "OK"} the array is empty.
I have abandoned using objects. the efficiency of converting objects to arrays in php is too low!
It's finally finished! The syntax format of the php two-dimensional array is not good, and it takes a lot of detours. Thank you very much for your help!
Post the code for your reference:
//---------------------------------------------------------------
1000) {die ('possible deep recursion attack');} foreach ($ array as $ key => $ value) {if (is_array ($ value )) {arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also);} else {$ array [$ key] = $ function ($ value );} if ($ apply_to_keys_also & is_string ($ key) {$ new_key = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} /*************************************** * ************************ convert an array to a JSON string (compatible with Chinese characters) * @ paramarray $ array the array to be converted * @ return string the converted json string * @ access public ****************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_encode ($ ar Ray); return urldecode ($ json) ;}?> Campus mobile APP
Navigation --- [collection result] 2:
10 [1] => 2166 [2] => creating a family of model employees for innovative trade unions [3] => College News [4] => if ($ myarrnews [$ I]! = "") {Echo "No $ I"; $ newslist [$ I] ['title'] = $ myarrline [2]; $ newslist [$ I] ['commentcount'] = 0; $ newslist [$ I] ['source'] = $ myarrline [7]; $ newslist [$ I] ['nid'] = $ myarrline [1]; $ newslist [$ I] ['replace count'] = 0; $ newslist [$ I] ['ptime'] = $ myarrline [9];} else {echo "no data has been collected for the current year! Maybe this site does not have the latest data! ";}}$ Data = array (" newslist "=> $ newslist," totalnum "=> 5); $ obj [" data "] = $ data; $ obj ["msg"] = "OK"; print_r (JSON ($ obj); $ newslist = NULL; $ obj = NULL;?> |