jcmaad Posted May 13, 2022 Share Posted May 13, 2022 (edited) Hi everyone, I need help writing a custom php function that will serialize three unique values in the following format. I have three values: Count, Total, Average and all three are numeric values. Here is an example of the output: a:3:{s:5:"count";i:1;s:5:"total";i:5;s:7:"average";d:5;} And this how it looks unserialized: { "count": 1, "total": 5, "average": 5 } You can call the values rating_count, rating_total, rating_average. Thank you in advance. Edited May 13, 2022 by jcmaad Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/ Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 (edited) Can you be a bit clearer on 'serialize'? Since you seem to show us what you need I am wondering what you want us to do. Oh. And just what is a "serialized function"? Edited May 13, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596207 Share on other sites More sharing options...
jcmaad Posted May 13, 2022 Author Share Posted May 13, 2022 Its a php function: https://www.w3schools.com/php/func_var_serialize.asp I need a function that will take three numeric values and output it in the serialized format. Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596208 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 And what have YOU tried to write so far? Post is here (using the <> icon of course) Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596209 Share on other sites More sharing options...
jcmaad Posted May 13, 2022 Author Share Posted May 13, 2022 Nothing, my knowledge of php is zero Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596210 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 I went to your link and have to ask "Are you kidding me?". The link shows you the code. There is no need for any additional function. It is right there. Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596211 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 If your knowledge of php is zero, then I suggest you find another line of work. Or start by getting a good book and reading it and following whatever tutorials they present. Beginning with serialzed data strings and custom functions is not exactly a starting point. And there are no 'serialized functions'. Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596212 Share on other sites More sharing options...
jcmaad Posted May 13, 2022 Author Share Posted May 13, 2022 So, I see a**holes from stackoverflow migrated to this forum? Why are you condescending ? Im not a coder and using this in my side wordpress project and i just need small piece of puzzle. "And there are no 'serialized functions'." clearly you are the one needs another line of work: https://www.php.net/manual/en/function.serialize.php Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596213 Share on other sites More sharing options...
ginerjm Posted May 13, 2022 Share Posted May 13, 2022 That is NOT a 'serialized function'. It is a STATEMENT that calls a function that does 'serializing'. My problem is with people who don't know how to ask a valid question. And people who have no idea what they are doing and want someone to do it all for them. The forum (here and everywhere) is to HELP people perform their own writing. If you want to hire someone to do that for you, go to another forum. I'll leave to your own devices now. BTW - did stackoverflow not give you what you were looking for, so you came here? Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596214 Share on other sites More sharing options...
Barand Posted May 13, 2022 Share Posted May 13, 2022 You'll find that serializing data it not used very much these days, largely abandoned in favour of JSON data $data = [ 'count' => 1, 'total' => 5, 'average' => 5 ]; echo "Serialized: " . serialize($data); echo "JSON: " . json_encode($data);; gives Serialized: a:3:{s:5:"count";i:1;s:5:"total";i:5;s:7:"average";i:5;} JSON: {"count":1,"total":5,"average":5} 1 Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596215 Share on other sites More sharing options...
jcmaad Posted May 13, 2022 Author Share Posted May 13, 2022 (edited) 10 minutes ago, Barand said: You'll find that serializing data it not used very much these days, largely abandoned in favour of JSON data $data = [ 'count' => 1, 'total' => 5, 'average' => 5 ]; echo "Serialized: " . serialize($data); echo "JSON: " . json_encode($data);; gives Serialized: a:3:{s:5:"count";i:1;s:5:"total";i:5;s:7:"average";i:5;} JSON: {"count":1,"total":5,"average":5} Thank you very much. Yes, from what I read its not the best way to handle data. My wordpress plugin is using this structure, so I have to abide by it. I modified your code to accept values, is this correct? $data = [ 'count' => $rating_count, 'total' => $rating_total', 'average' => $rating_average ]; Edited May 13, 2022 by jcmaad Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596216 Share on other sites More sharing options...
Barand Posted May 13, 2022 Share Posted May 13, 2022 1 hour ago, jcmaad said: is this correct? Possibly. How can I know? Syntax is OK. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596218 Share on other sites More sharing options...
jcmaad Posted May 13, 2022 Author Share Posted May 13, 2022 Yup, I was asking about syntax. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/314786-need-help-with-building-a-custom-serialized-function/#findComment-1596219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.