I have a CSV file which needs some additional processing. I've got most of our custom functionality completed. My stuck at the moment is the latest addition to the feed, multiple categories in 1 column. Here is a quick example of the new field setup.
Category01#Things~Category01#Will~Category01#Be~Category01#Here~Category02#Testing~Category02#More text here~Category02#Any data~Category02#No more data for this category~LastCategory#This~LastCategory#Is~LastCategory#The~LastCategory#End
I would need to build an array in PHP from each category available, similar to;
$category01 = array('Things', 'Will', 'Be', 'Here');
Any help would be greatly appreciated. Thanks!
If I understand your question and the format correctly, categories separated by ~, and each listed as "SomeString#Category Name", then this should to the trick. However I don't think this has anything to do with the CSV format.
$pairs = explode('~', $string);
$cats = array();
foreach ($pairs as $pair) {
list($cat_number, $cat_name) = explode('#', $pair);
$cats[] = $cat_name;
}
Gahhh. the goggles, they do nothing!
If you're unable to change that output form (and it SHOULD be changed to something nicer), you'll have to go brute force:
$csv = '...';
$categories = array();
$parts = explode('~', $csv);
foreach($parts as $part) {
$bits = explode('#', $part);
$category = (int)substr($part[0], 8);
if (!is_array($categories[$category])) {
$categories[$category] = array();
}
$categories[$category][] = $part[1];
}
Of course, this'll blow up on your LastCategory stuff at the tail end of that "csv". so... let me again STRONGLY urge you fix up whatever's generating that so-called "csv" in the first place.
Related
I am a newbie in this and I have read lots of stuff about this matter (including some topics here), before starting this topic, but I do not quite get it yet, so I will ask for some help (if it is possible) :)
So, in the column that I want to print I have values like this on every row:
value1|value2|value5|value12|value25
value3|value5|value12|value14|value26|value32|value55
value1|value2|value14|value26|value31
The number of rows can be 3 or 1500+... So I want to merge the arrays and print those values sorted and without duplicates: value1, value2, value3, value5, value12, etc...
I have tried to explode the arrays, but I could not find out how to assign a variable to every array and merge them and all I have done is to print all values:
foreach ($rows as $areas) {
foreach (explode('|', $areas->value) as $area) {
var_dump($area);
}
}
Afterwards I have read somewhere this will be very slow if I have many rows (and I am going to have thousands), so I am stuck here and I do not know what else I could do...
I will appreciate any help and direction that you can give me, because it is too hard for me and I can not do it without help
Thank you in advance
You can store each value of your exploded string as key (if it's not an object nor array), it store only unique values. Then you have to just use array_keys() to get keys and sort returned array:
$rows = array(
'value1|value2|value5|value12|value25',
'value3|value5|value12|value14|value26|value32|value55',
'value1|value2|value14|value26|value31'
);
$results = array();
foreach ($rows as $row) {
$items = explode('|', $row);
foreach ($items as $item) {
$results[$item] = 0;
}
}
$results = array_keys($results);
sort($results, SORT_NATURAL);
Live demo on eval.in
There are two ways of doing this:
<?php
$str = 'value1|value2|value5|value12|value25';
$str1 = 'value3|value5|value12|value14|value26|value32|value55';
$str2 = 'value1|value2|value14|value26|value31';
//-- Method 1: Concat and make a single string and then explode and make a single array
$finalString = $str . '|' . $str1 . '|' . $str2;
print_r(array_unique(explode('|', $finalString)));
//-- Method 2: explode first and then merge into a single array
$strArr = explode('|', $str);
$strArr1 = explode('|', $str1);
$strArr2 = explode('|', $str2);
print_r(array_unique(array_merge($strArr, $strArr1, $strArr2)));
I have two files that I need opened, I'm using php file to read them
$lines = file('/home/program/prog_conf.txt');
foreach ($lines as $line) {
$rows = preg_split('/\s+/', $line);
Followed by:
$lines = file('/home/domain/public_html/base/file2.cfg');
foreach ($lines as $line) {
$rows = preg_split('/=/', $line);
As I work with these two files, I need to pull info from the second one, which I seperated by =, however, I'm not sure this is the best thing to do. I wanted to add data checking from the database. The db details are in the second file like so:
dbname = databasename
dbuser = databaseuser
dbpass = databasepassword
If I echo the $rows[2], I get everything all the information I need on a single line, not on seperate lines. Meaning:
databasename databaseuser databasepassword
How do I split the information up so I can use the entries one by one?
How about:
$lines = file('/home/domain/public_html/base/file2.cfg');
$all_parts = array()
foreach ($lines as $line) {
//explode pulls apart a string based on the first value, so you could change that
//to a '=' if need be
array_merge($all_parts,explode(' ', $line));
}
This would get you all the parts of the file, one at a time, into an array. Which is what I think you wanted.
If not, just explode as needed
Maybe this aproach helps:
First as i see your second file has multiple lines, so what would do is something like this:
Assumin that every key as "db" in common we can do something like this.
$file = fopen("/home/domain/public_html/base/file2.cfg", "rb");
$contents = stream_get_contents($handle); // This function return better performance if the file isn't too large.
fclose($file);
// Assuming this is your return from the file
$contents = 'dbname = databasename dbuser = databaseuser dbpass = databasepassword';
$rows = preg_split('/db+/', $contents); // Splinting keys "db"
$result = array();
foreach($rows as $row){
$temp = preg_replace("/\s+/", '', $row); // Removing extract white spaces
$temp = preg_split("/=/", $temp); // Splinting by "="
$result[] = $temp[1]; // Getting the value only
}
var_dump ($result);
I hope this help you can try this code maybe with little modifications but works.
Is there an easy way to parse the following data that I will post below. The data comes from the web.
I was using the $rows = explode("\n", $txt_file); then the $parts = explode('=', $line_of_text); to get the key name and values. However, I don't know how to handle the extra information that I do not want.
Additionally, I do not know how to get rid of the extra spaces. The file seems to be made for some kind of easy parsing. I have looked all over this site to find a solution. However, this data is quite different than the examples I have found on this site.
# This file holds all the timelines available at this time.
# All lines starting with # is ignored by parser...
#
STARTINFO
description = Rubi-Ka 2
displayname = Rimor (Rubi-Ka 2)
connect = cm.d2.funcom.com
ports = 7502
url =
version = 18.5.4
ENDINFO
STARTINFO
description = Rubi-Ka 1
displayname = Atlantean (Rubi-Ka 1)
connect = cm.d1.funcom.com
ports = 7501
url =
version = 18.5.4
ENDINFO
You can use the trim function to get rid of the whitespace.
To only keep the columns you want, you can store their keys in an array, and make a check against it when parsing.
Here's an example (albeit rather verbose).
<?
$lines = explode("\n", $data);
$result = array();
$count = 0;
// an array of the keys we want to keep
// I have the columns as keys rather then values for faster lookup
$cols_to_keep = array( 'url'=>null, 'description'=>null, 'ports'=>null, 'displayname' => null);
foreach($lines as $line)
{
//skip comments and empty lines
if(empty($line) || $line[0] == '#')
{ continue; }
//if we start a new block, initalize result array for it
if(trim($line) == 'STARTINFO')
{
$result[$count] = array();
continue;
}
// if we reach ENDINFO increment count
if(trim($line) == 'ENDINFO')
{
$count++;
continue;
}
//here we can split into key - value
$parts = explode('=', $line);
//and if it's in our cols_to_keep, we add it on
if(array_key_exists(trim($parts[0]), $cols_to_keep))
{ $result[$count][ trim($parts[0]) ] = trim( $parts[1] ); }
}
print_r($result);
?>
I need to be able to parse this sort of data in PHP:
Acct: 1
email : bleh#gmail.com
status : online
--------------------------------------------------
Acct: 2
email : dfgg#fgfg.com
status : banned
--------------------------------------------------
Acct: 3
signedupname : SomeUsername
somethingelse : offline
--------------------------------------------------
As you can see the data is largely random. The only thing that remains the same is the ----- seperating each entry and the Acct: 1 bits. The padding between each : often changes as does the variable to represent each bit of data.
I've tried going through and parsing it all myself using regex but I am defenately not skilled enough to be able to do it properly. At the end of this I want data according to the following:
Acct: <integer>
var1: <string>
var2: <string>
If that makes any sense at all. It doesn't need to be that effeciant, as I will need to do this about once a day and I do not mind waiting for how-ever long it needs.
Thank you. :)
Edit: This is what I did on my own:
<?php
$data = file_get_contents('records.txt');
$data = explode('******** Saved Host list with acct/variables ********', $data);
$data = explode('--------------------------------------------------', $data[1]);
foreach($data as &$dataz)
{
$dataz = trim($dataz);
}
$data = str_replace('Acct:', "\nAcct:", $data);
foreach($data as $dataz)
{
preg_match('/Acct: (.*)/', $dataz, $match);
$acct = $match[1];
preg_match('/: (.*)/', $dataz, $match);
$var1 = $match[1];
echo $var1;
}
?>
I got as far as extracting the Acct: part, but anything beyond that I simply can't get my head around.
This piece of code will take your entire input and produce an associative array for each record in your input:
// replace ----- with actual number of dashes
foreach (explode('-----', $input) as $entry) {
$entry = trim($entry);
$record = array();
foreach (explode("\n", $entry) as $line) {
$parts = explode(':', $line);
$varname = trim($parts[0]);
$value = trim($parts[1]);
$record[$varname] = $value;
}
// Do anything you want with $record here
}
Edit: I just had a look at the code you posted. You really don't need regular expressions for what you're trying to do. Regex can be really handy when used in the right place, but most of the time, it's not the right thing to use.
How can i array a string, in the format that $_POST does... kind of, well i have this kind of format coming in:
101=1&2020=2&303=3
(Incase your wondering, its the result of jQuery Sortable Serialize...
I want to run an SQL statement to update a field with the RIGHT side of the = sign, where its the left side of the equal sign? I know the SQL for this, but i wanted to put it in a format that i could use the foreach($VAR as $key=>$value) and build an sql statement from that.. as i dont know how many 101=1 there will be?
I just want to explode this in a way that $key = 101 and $value = 1
Sounds confusing ;)
Thanks so so much in advanced!!
See the parse_str function.
It's not the most intuitive function name in PHP but the function you're looking for is parse_str(). You can use it like this:
$myArray = array();
parse_str('101=1&2020=2&303=3', $myArray);
print_r($myArray);
One quick and dirty solution:
<?php
$str = "101=1&2020=2&303=3";
$VAR = array();
foreach(explode('&', $str) AS $pair)
{
list($key, $value) = each(explode('=', $pair));
$VAR[$key] = $value;
}
?>
parse_str($query_string, $array_to_hold_values);
$input = "101=1&2020=2&303=3";
$output = array();
$exp = explode('&',$input);
foreach($exp as $e){
$pair = explode("=",$e);
$output[$pair[0]] = $pair[1];
}
Explode on the & to get an array that contains [ 101=1 , 2020=2 , 303=3 ] then for each element, split on the = and push the key/value pair onto a new array.