Jump to content

Need help with building a custom serialized function


jcmaad

Recommended Posts

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 by jcmaad
Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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}

 

  • Like 1
Link to comment
Share on other sites

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 by jcmaad
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.