The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 02:23:22 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Trying to sort a datafeed with PHP  (Read 2388 times)
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« on: April 25, 2009, 08:28:41 AM »

I'm trying to sort some products ( ~ 1K-2K) from a datafeed !

Is this doable with PHP ?

Let's say for example that I have a file with all products codes (codes.txt) and the datafeed (datafeed.txt) !

Can I build a script to import from codes.txt all the codes and search them in datafeed.txt and then select the whole lines of the codes found in codes.txt and to export them in a new file called results.txt ?

I'm thinking how to write a script !

I've managed to do this with Notepad++ but it require to enter each code on the search box and to type 1K-2K codes is kinda of boring ! 

Please help  Nerd
Logged

what's up?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 25, 2009, 11:27:56 AM »

Yes of course you can, trivial actually. I'm not going to code it for you, I'm going to tell you sequencing and tools in the toolbox to execute. This is, of course, just one way to skin the cat.

  • use file_get_contents to import the text file.
  • use explode to convert the lines into individual array entries.
  • use foreach() to iterate through each line in the input array.
  • use preg_match() to extract from each line, the code information you want (of course this step is not necessary if the line IS the code...)
  • take the part(s) you extracted from the input line and add it to another array.
  • use sort or asort or arsort or ksort et al to sort your array.
  • use implode() to convert the array into a single block of text.
  • use file_put_contents() to move the string back out to a file.

Extra points:
If you are going to be using this array a lot during web calls, then rather than exploding/imploding it, use serialize/unserialize to save it in a more quickly imported way. Extra credit if you use try/catch to see if the input file is in serialized format or straight text and behave accordingly.
« Last Edit: April 25, 2009, 11:36:11 AM by perkiset » Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #2 on: April 25, 2009, 11:35:26 AM »

I'm not going to code it for you, I'm going to tell you sequencing and tools in the toolbox to execute.
I don't want to code it for me  Grin ! I want to learn !

Thanks for the tips !

Back to work 
Logged

what's up?
NYDAz
Expert
****
Offline Offline

Posts: 212

The Night Stalker


View Profile
« Reply #3 on: April 25, 2009, 12:16:49 PM »

file_get_contents for both files, right ?
Logged

what's up?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: April 25, 2009, 12:28:55 PM »

file_get_contents is content-agnostic, meaning that you can technically load up just about anything with it. So yes, whether you are loading a serialized array, or textual content, or even a binary file for that matter, it's a proper tool.

Bear in mind: not always the right tool, like when you're dealing with huge files, but an excellent tool in many cases.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: April 25, 2009, 12:31:38 PM »

I forgot one even better way to do this, for extra-extra credit if you want to go really fast and have to load this file all the time.

Save your serialized array in a string buffer that looks like this:

<?php

$myArr = unserialize(' [put the serialized array string here] ');

?>

then include that as code, rather than doing a file_get_contents and such. The best-best benefit is if you're using a cache mechanism like APC, because it will be compiled and ready to go all the time.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!