I want a value of my array to be displayed if I use a equel or almost equel variable.
So for example if I have the following array line: [1] => g
I want to display 'g' if I use the variable $1 (Or even better with the varible $arr1, so it does not interfere with other things later on.)
Here is my code: (I'm uploading a simple .txt file with some letters and making a array of each individual charachter):
$linearray = array();
$workingarray = array();
while(! feof($file)) {
$line = fgets($file);
$line = str_split(trim("$line"));
$linearray = array_merge($linearray, $line);
}
$workingarray[] = $linearray;
print_r($workingarray);
When I have done this I will get this outcome;
Array ( [0] => Array ( [0] => g [1] => g [2] => h [3] => o [4] => n
[5] => d [6] => x [7] => s [8] => v [9] => i [10] => s [11] => h [12]
=> f [13] => g [14] => f [15] => h [16] => m [17] => a [18] => g [19] => i [20] => e [21] => d [22] => h [23] => v [24] => b [25] => v [26] => m [27] => d [28] => o [29] => m [30] => v [31] => b [32] => ) )
I tried using the following to make it work:
extract($workingarray);
echo "$1";
But that sadly doesn't work. I just recieve this:
$1
And I want to recieve this:
g
It would be even better if I recieved the same effect with for example echo "$arr1" and then recieve g and for echo "$arr2" recieve h etc etc
This is simply impossible: http://php.net/manual/en/language.variables.basics.php
Variable names cannot start with a digit. The only allowable first char for variable names are letters and underscore.
And don't use extract or similar constructs. All they do is litter your variable namespace with unpredictable/unknown junk - you could very easily overwrite some OTHER critical variable with this useless junk, making for very difficult/impossible bugs to diagnose.
You're not saving any time by making up these new variables.
Related
i am writing a script to reach out to a website that downloads a csv into cache and then parses the data into an array.
$base_url = "http://www.collincad.org/ccad/propertysearch/download.php?situs_num=1707&situs_street=university&situs_street_suffix=&isd%5B%5D=any&city%5B%5D=any&prop_type%5B%5D=R&prop_type%5B%5D=P&prop_type%5B%5D=MH&active%5B%5D=1&year=2018";
$handle = fopen($base_url, "r");
$flag = true;
while(!feof($handle))
{
$text = fgetcsv($handle, 1024, ",");
if($flag) { $flag = false; continue; }
print $text[1]. " <br>";
}
mysql_close($connect);
When performing the query this way it has the first row and a row of other data and ignores the comma.
$base_url = "export5.csv";
$handle = fopen($base_url, "r");
$flag = true;
while(!feof($handle))
{
$text = fgetcsv($handle, 1024, ",");
if($flag) { $flag = false; continue; }
print $text[1]. " <br>";
}
mysql_close($connect);
but when i manually download the csv file it and read it from the local folder it works as expected... i would prefer not to make this a two step process... im thinking that reading direct from the site with php is the issue, just can figure out how to resolve it.
Thanks
First and foremost when using fopen with a web url, make sure your server is configured to allow it (http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen). Once that is out of the way you should be fine with your code.
The issue though is the CSV format itself.
Looking at the CSV return of that url, its delimiters are tabs, not commas. And I see no enclosures too. So you need to change your fgetcsv to:
$text = fgetcsv($handle, 1024, "\t", '');
And it should begin to return results like this (for each $text):
Array
(
[0] => 15071
[1] => 2018
[2] => P
[3] => Personal
[4] => P-9000-288-0243-1
[5] => N
[6] => ZZZZZZZ BPP # 1707 W UNIVERSITY DR
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] => BPP AT 1707 W UNIVERSITY DR
[13] => KROGER #488
[14] => 1707
[15] => UNIVERSITY DR
[16] => MCKINNEY
[17] => 1707 W University Dr | McKinney, TX 75069
[18] => 844925
[19] => THE KROGER CO
[20] => CMC
[21] => MCKINNEY CITY
[22] => SMC
[23] => MCKINNEY ISD
[24] =>
[25] => Active
[26] => No
[27] =>
)
Also, the first line in the csv file is this:
Line 1:
Array
(
[0] => sep=
[1] =>
)
So you may want to skip the first TWO lines (the second line being the column headers).
Line 2: (column headers)
Array
(
[0] => Property ID
[1] => Year
[2] => Property Type Code
[3] => Property Type Description
[4] => Geographic ID
[5] => Abstract Or Subdivision Code
[6] => Abstract Or Subdivision Description
[7] => Block
[8] => Tract Or Lot
[9] => Mobile Home Park Code
[10] => Mobile Home Park Description
[11] => Mobile Home Park Space
[12] => Legal Description
[13] => Doing Business As
[14] => Street Number
[15] => Street Name
[16] => City
[17] => Complete Address
[18] => Owner ID
[19] => Owner Name
[20] => Taxing City Code
[21] => Taxing City Name
[22] => Taxing School District Code
[23] => Taxing School District Name
[24] => Market Value
[25] => Property Status
[26] => Certified Data
[27] =>
)
I have created a multi-dimensional array using fgetcsv from a CSV file.
Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document.
The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct.
I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML.
An example of the code is below, product is the multi-dimensional array.
public function array_to_xml($product, &$xml)
{
foreach($product as $row)
{
$element = $xml->createElement("Product");
$xml->appendChild($element);
$element = $xml->createElement("ID", ($row[38]));
$xml->appendChild($element);
}
}
The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated.
The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements.
<?xml version="1.0"?>
<ProductList/>
<Product>
<ID/>
</Product>
</ProductList>
Example of $row when printed to screen:
Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 )
A couple things stand out. First, you have a syntax error on this line:
$element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter.
Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this.
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$id = $xml->createElement("ID", $row[38]);
$product->appendChild($id);
$xml->appendChild($product);
}
}
If you need it as an attribute as #Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like:
public function array_to_xml($product, &$xml)
{
foreach ($product as $row)
{
$product= $xml->createElement("Product");
$product->setAttribute('ID', $row[38]);
$xml->appendChild($product);
}
}
I'm new here, and I have a question. I'm doing a code that I'll use soon, more something left me with a huge doubt. So I'm separating the word more special characters converted are being separated, I wish they would get together to assign a color to each then is there any way to do this?
Code:
<?php
$text = "My nickname is: п€Яd Øwп€d"; #this would be the result of what i received via post
print_r(str_split($text));
?>
Result:
Array
(
[0] => M
[1] => y
[2] =>
[3] => n
[4] => i
[5] => c
[6] => k
[7] => n
[8] => a
[9] => m
[10] => e
[11] =>
[12] => i
[13] => s
[14] => :
[15] =>
[16] => &
[17] => #
[18] => 1
[19] => 0
[20] => 8
[21] => 7
[22] => ;
[23] => &
[24] => e
[25] => u
[26] => r
[27] => o
[28] => ;
[...]
)
I'd like to return this:
Array ( [0] => M
[1] => y
[2] =>
[3] => n
[4] => i
[5] => c
[6] => k
[7] => n
[8] => a
[9] => m
[10] => e
[11] =>
[12] => i
[13] => s
[14] => :
[15] =>
[16] => п
[17] => €
[...]
)
Thank you for the help.
[UPDATED]
I tested the functions that friends have passed, most don't use utf-8 as my default charset ISO-8859-1, and one more thing I forgot to add, by editing the phrase "My nickname is:" and adding a & for example: "My nickname is & personal name" returns a bug. I appreciate who can help again.
You can try to write your own str_split, which could look like the following
function str_split_encodedTogether($text) {
$result = array();
$length = strlen($text);
$tmp = "";
for ($charAt=0; $charAt < $length; $charAt++) {
if ($text[ $charAt ] == '&') {//beginning of special char
$tmp = '&';
} elseif ($text[ $charAt ] == ';') {//end of special char
array_push($result, $tmp.';');
$tmp = "";
} elseif (!empty($tmp)) {//in midst of special char
$tmp .= $text[ $charAt ];
} else {//regular char
array_push($result, $text[ $charAt ]);
}
}
return $result;
}
Basically what it does is check if the current character is a &, if so, save all following characters (including ampersand) in $tmp until ;. This basically gives you the wanted result but will fail, whenever there is a & which doesn't belong to an encoded character.
Use preg_split():
<?php
$text = "My nickname is: п€Яd Øwп€d"; #this would be the result of what i received via post
print_r(preg_split('/(\&(?=[^;]*\s))|(\&[^;]*;)|/', $text, -1, PREG_SPLIT_DELIM_CAPTURE + PREG_SPLIT_NO_EMPTY));
?>
i have an array like
$newArray = $_POST[$newId];
print_r($newArray);
it prints like
Array ( [1] => Yes [2] => a [3] => b [4] => c [5] => d [6] => e [7] => f [8] => [9] => [10] => [11] => [12] => [13] => [14] => )
but when i try to store in in db after serializing like
serialize($newArray)
it get stored like
s:211:"Array
(
[1] => Yes
[2] => ab
[3] => c
[4] => d
[5] => e
[6] => f
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] =>
[14] =>
)
";
which is a single array element in DB..how do i properly serialize the element.
It looks like it's serializing a string, not an array. Are you sure $newArray is an array?
The string returned from serialize starts with 's:211'. This means that a string was passed into serialize(). If an array were passed into serialize() the returned string would start with 'a:14'.
#pradeep
where you storing $newArray in textfield, store it by serialling
$arrayString = $_POST['newId'];
You will get seriallized array in $arrayString.
If you want to use that array before storing in database , use unserialize , else directly store in database as that is already seriallized.
$array = unserialize($arrayString);
This will solve your problem
Not really sure I understand the question, if you don't want to serialize though, and if you want to pass it from a text field, maybe do custom syntax like - 1:a;b:2;c:3
then explode(';',$string); loop that and for the result, explode(':',$rows)
make the delimiters more difficult to clash
explode("[[;]]", string); // 1]]:[[b[[;]]
I got a problem when I tried to find some characters with following code:
$str = "统计类型目前分为0日Q统计,月统q计及287年7统1计三7种,如需63自定义时间段,点1击此hell处进入自o定w义统or计d!页面。其他统计:客服工作量统计 | 本周服务统计EXCEL";
preg_match_all('/[\w\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A]/',$str,$match); //line 5
print_r($match);
And I got error as below:
Warning: preg_match_all() [function.preg-match-all]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 4 in E:\mycake\app\webroot\re.php on line 5
I'm not so familiar with reg expression and have no idea about this error.How can I fix this?Thanks.
The problem is, that the PCRE regular expression engine does not understand the \uXXXX-syntax to denote characters via their unicode codepoints. Instead the PCRE engine uses a \x{XXXX}-syntax combined with the u-modifier:
preg_match_all('/[\w\x{FF10}-\x{FF19}\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}]/u',$str,$match);
print_r($match);
See my answer here for some more information.
EDIT:
$str = "统计类型目前分为0日Q统计,月统q计及287年7统1计三7种,如需63自定义时间段,点1击此hell处进入自o定w义统or计d!页面。其他统计:客服工作量统计 | 本周服务统计EXCEL";
preg_match_all('/[\w\x{FF10}-\x{FF19}\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}]/u',$str,$match);
// ^
// |
print_r($match);
/* Array
(
[0] => Array
(
[0] => 0
[1] => Q
[2] => q
[3] => 2
[4] => 8
[5] => 7
[6] => 7
[7] => 1
[8] => 7
[9] => 6
[10] => 3
[11] => 1
[12] => h
[13] => e
[14] => l
[15] => l
[16] => o
[17] => w
[18] => o
[19] => r
[20] => d
[21] => E
[22] => X
[23] => C
[24] => E
[25] => L
)
) */
You're sure, that you used the u-modifier (see arrow above)? If so, you'd have to check if your PHP supports th u-modifier at all (PHP > 4.1.0 on Unix and > 4.2.3 on Windows).