I can't find this anywhere. I have some old basic programs I am working on (thanks to qb64 that came out, now they work on winxp - win7)
in order to serialize (like php) I need to know how this process works so that i can convert BASIC do it. it does not have to be fancy, but I would like to get an understanding how it works.
I like the way php does it, although since BASIC can not do 'associative" arrays, i would think it is much easier.
so in simple terms, is there a source for serialize/unserialize ?
looks like you'd serialize it with simple string concatenation. Use something like "||" as your seperator. Since there are no associative arrays, you don't have to worry about names, just value.
Then you'd use instr() and left$() or mid$() to split them back out.
For multidimensional arrays, it would be considerably more complex and I haven't given it the time to figure out exactly how I'd do it, but I thought about using seperatators like ||0|0|| for array(0,0) and ||0|1|| for array(0,1) or even ||0|1|1|| for array (0,1,1)
Related
I am trying to find the correct (or best) content type to pass to header() when outputting serialized PHP data. The best I could find when searching was a proposal for application/vnd.php.serialized and another person asking the same question without a definitive answer. I was considering using just text/plain and moving on.
The data being serialized is an associative array, 1-3 levels nested. Values are always string/int/bool, in order of occurrence.
For reasoned I'd rather not get into, my application is using PHP4. Otherwise I would be using JSON and be on my way.
Edit: My question is not about how to work with JSON in PHP.
For those interested: I am handling JSON encode/decode using the 'Services_JSON' PEAR package , however I've noticed performance issues which PHP serialized resolves.
I did not find a real official answer, but the best matching answer I can find is:
application/php-serialized
or better
application/php-serialized; charset=UTF-8
I've used Google, Yahoo, AND Bing, but I can't find any good answers. I've seen jLinq, but I want to be able to query JSON in PHP in hopes of having a not an SQL database, but instead all data storage within the filesystem on my server. No, I don't care how bad it sounds.
Ideas nonetheless? I would think that there would be a PHP class on this.
-----EDIT-----
Guys, thanks for your answers so far, but I don't think that json_encode and json_decode are of much use. What I want to be able to do is encode/decode JSON, and be able to search it for specific keys with specific values. Albeit I have PROVEN to myself that I can do so, it's a lot of code for something that should be so simple. Anything else you have in mind?
You can use JsonQ package. This package can query over JSON data like Query Builder.
https://github.com/nahid/jsonq
You can call json_encode to convert your data to a string, and write this string to a file. Then when you want to use it, you read the entire file and call json_decode to convert it back to data. When you're done processing it, repeat the encode/write steps.
But if you have multiple processes doing this, they'll completely overwrite what each other is doing. So it's not a very good way to manage shared data.
You could try JSONPath it allows you to query json with xpath as you would xml.
Look into json_decode. This lets you take JSON as a string and parse it into an object in PHP. You could theoretically store the strings as files, and use file_get_contents to retrieve the string from the file.
You would have to write your own searching/indexing/updating/etc algorithms, but if you don't want to use a real database solution (since you said, No, I don't care how bad it sounds.), then I guess this would work.
To search for values in the object you get from json_decode, look into in_array and array_search
I’ve been playing around with searching text in big lists and found that using a PHP array seems to be a quick way of doing it.
E.g. if you had loads of place names and associated postcodes you could read them into a PHP array like this:
$place[‘place name here’] = “postcode”;
Then to look up you just take the place you want to look up and plug it in to the array:
$postcode_sought = $place[‘place I want to look up’];
I thought I could speed this up using C++ but of course C++ does not allow (as far as I know) arrays with a string as the index.
The only way I can think to do it is to create vectors for the place and postcode and loop through the place vector looking for a match but the repeated string comparisons take forever as I'd expected. I also experimented with hashing the text but I still couldn’t get it anywhere near as fast as PHP.
I think PHP is written in C so my question is how does C manage to create this string index name functionality for PHP?
I’m not looking for the actual code or anything, it just seems to me that there must be some fundamental technique that is used for this and I was just wondering if there is anyone out there who could briefly explain it.
Thanks in advance.
C
I thought I could speed this up using C++ but of course C++ does not allow (as far as I know) arrays with a string as the index.
It does, You can use std::map as an Associative array.
You could try using Berkeley DB. Back in the days it was the fastest but by default it's disk oriented. I don't know if you can run it in memory but you can always mount the directory from tmpfs.
PHP propably uses some external class for hashing table. You can get quite far by writing a quicksearch algorithm. Sort the keys and check up the key in the middle. Then again in middle until you've found the key. You can also use MD5() for keys as it's faster than pure string comparison.
C and C++ only allow integer types to be array indexes, and strings aren't even a type on C/C++, they're actually an array of chars.
As stated above, use std::map or similar.
I just started doing jQuery last week, and so far I already made some basic systems with ajax, like basic jQuery CRUD and simple chat system without referencing on other's work for I decided to test myself on how far I can do systems alone in jQuery(without JSON and XML yet).
But when I decided to look at other's work (hoping to get/learn good practices and codes out there) many or almost every program that deals with ajax have some JSON in it. So I decided to study and read JSON specially this one, but I guess because it's my first time dealing with it, I'm having a problem sinking it into my brain. Yeah I know it is a "lightweight way of describing hierarchical data", I also know how to make JSON like mixing a literal array and object in JS, and how to dsplay it in js.
But my question is, what's the difference and what's the advantage than not using it?
When I can still get and store data on the server using ajax and database without JSON.
By the way I haven't focus on XML yet because based from my research it's better to use JSON in AJAX.
Can you give me some actual scenario dealing with
s1. ajax php mysql (this with what disadvantages?)
and
s2. ajax php mysql json (this with what advantages?)
I mean, my focus is to send and get data, and I already can do it with s1.
Sorry if you find my question stupid. Tia. :)
Why use JSON? The answer is portability and structure.
JSON is portable because parsers and writers are available for many, many languages. This means that JSON that a PHP script generates can be very easily understood by a JavaScript script. It is the best way to transmit complex structures like arrays and objects, and have it still be compatible with multiple languages.
JSON provides structure because the data you transmit with it can have consistent formatting. This is instead of transmitting back plain-text (i.e. unformatted) data, like comma-separated or delimited data.
Data that is merely delimited (for example, "BookName1,BookName2,BookName3") is more difficult for humans to understand, debug, and work with. If you wanted to debug a response between your server and your browser and the data was delimited (like my example above), you might have a hard time understanding it. Also, if you want to add different data types, provide separate records, etc., then your custom data format becomes more complicated. Eventually, you might end up reinventing JSON.
As a side note, JSON is indeed better than XML. It is much more efficient space-wise. There are no tag names to take up space. Structure is created via nested braces, instead of verbose tags.
Resources
Here is an interesting article on the differences and pros/cons of XML and JSON: http://www.json.org/xml.html
Examples
Per your request, here is an example of encoding JSON with PHP. This is ripped from the docs:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Contrast this to something like this, without JSON:
a,1
b,2
c,3
d,4
e,5
To parse that, you'd have to iterate through each line, split the values yourself, and then create the array. This isn't that difficult, but imagine you have a nested object:
$arr = array ('a'=> array(1,2,3),'b'=> array('a' => 1, 'b' => 2),'c'=>3,'d'=> array(1,2,3,4,5) ,'e'=>5); // etc.
With JSON, it's no different to encode it. Just use json_encode. But, encoding this manually, and then decoding it manually would be significantly more work.
Programming in any sort of programming language, you have several different types of data at your disposal, including the very useful array type.
Interchanging data between Javascript and any server side language can only happen through strings. I.e. you can send and return any text, but there's no way to send a native array or number type.
JSON is an elegant way to express array and other types using only a string. This way you can pass arbitrary data back and forth between different environments and are not limited to pure text. XML solves the same kind of problem, but is often overkill for simple AJAX requests.
I am new to php and am asking for some coding help. I have little experience with php and have gone to the php.net site and read couple books to get some ideas on how to perform this task.
There seems to be many functions and I am confused on what would be the best fit. (i.e. fgetcsv, explode(), regex??) for extracting data in the file. THen I would need assistance printing/display this information in orderly fashion.
Here is what I need to do:
import, readin txt file that is
delimited (see sample)
The attributes are not always ordered and some records will have missing attributes.
Dynamically create a web table (html)
to present this data
Sample records:
attribute1=value;attribute2=value;attribute3=value;attribute4=value;
attribute1=value;attribute2=value;attribute4=value;
attribute1=value;attribute2=value;attribute3=value;
How do I go about this? What would be best practice for this? From my research it seems I would create an array? multidimensional? Thank you for your time and insight and i hope my question is clear.
Seems like homework, if so best to tag it as such.
You will want to look into file(), foreach() and explode() given that it is delimited by ;
The number of attributes should not matter if they are missing, but all depends on how you setup the display data. Given that they are missing though, you will need know what is the largest amount of attributes to setup the table correctly and not cause issues.
Best of luck!
i would first use the file() method, which will give you an array with each line as an element. Then a couple of explodes and loops to get through it all,first exploding on ';', then loop through each of these and explode on '='.