possible limitation of implode function in PHP - php
I have the following code that is not returning as I expected. I was hoping the final result would be a string:
$organizers = array_unique($organizers); // this returns correctly
$organizers = implode(', ', $organizers); // this returns nothing
var_dump($organizers); // no data appears here
exit;
The array_unique() function is returning data correctly and I can see the array it returns. To start, the $organizers array is a simple 1-D array of strings that all have small lengths under 20 chars. I think the issue might be that $organizers is more than 10,000 indices long. Are there limitations on the length of an array that can be imploded? Are there work-arounds for that? I cannot find anything in the manual, but I have tested this code thoroughly and I believe the error must be on implode().
I dont' know if there is a limitation, but what comes to my mind is taht you are also transforming an array into a string. This shouldn't be the problem in PHP, but try calling it a different variable for the result of implode?
$organizers = array_unique($organizers); // this returns correctly
$organizers_string = implode(', ', $organizers); // this returns nothing
// This gives it a different space
Edit: And if for some reason implode() is still problematic.
$organizers = array_unique($organizers);
$neworganizers = "";
for($i = 0; $i < sizeof($organizers); $i++)
{
$neworganizers .= $organizers[$i];
if($i != sizeof($organizers) - 1)
{
$neworganizers .= ", ";
}
}
//$neworganizers is now the equivalent of what .implode() should return when called on $organizers
$organizers = array();
$organizers[0] = "value1";
$organizers[1] = "value2";
$organizers[2] = "value3";
$organizers[3] = "value3";
$organizers = array_unique($organizers); // strips out last index
$organizers = implode(', ', $organizers); // returns string of "value1, value2, value3"
echo $organizers;
This seemed to work on writecodeline.com/php/
I've also experienced issues with older php builds when I've tried to explode/implode by a string with special characters in it and they were encapsulated by single quotes. I know it sounds crazy, but the double quotes might be necessary on some servers.
Reference: personal experience doing work on older production servers.
I'd hate to think I'm stating the obvious, but doesn't implode only take a string as an argument? Maybe it should be something more like this...
$organizers = array_unique($organizers);
//I'm guessing what you wanted was an array of arrays?
$neworganizers = array();
for($i = 0; $i < sizeof($organizers); $i++)
{
$neworganizers[$i] = implode(", ", $organizers);
}
print_r($neworganizers);
Related
Compare string from input field with value in multidimension array
$klasseinput = strtoupper(trim($_POST["klasseliste"])); $data = file('student.txt'); $data = array_filter($data); foreach($data AS $row){ $student[] = explode(';', $row); } $antall = count($student); for ($i = 0; $i < $antall; $i++){ if($klasseinput == $student[$i][3]){ print('<tr><td>'.$student[$i][0]."</td><td>".$student[$i][1]."</td><td>".$student[$i][2]."</td><td>".$student[$i][3]."</td></tr>"); } } /////////STUDENT.txt////////// ph;petter;hanssen;IT1 gb;Geir;Bjarvin;IT2 mj;Marius;Johansen;IT3 ///////////////////////////// I am trying to compare an input form with an item in the multidimension array, but even tho the variable from the input field is exactly the same as the value in the array, it doesnt pass the if check. $student[0][3] = IT1 $student[1][3] = IT2 $student[2][3] = IT3
If you have made sure there is no white space spoiling the comparison, then you might find a function like this useful to look at the strings on both sides of the comparison. You might find there are spurious characters causing trouble. function hexdump($str) { for($i=0; $i<strlen($str);$i++) { echo "[$i] [".bin2hex($str[$i])."] [".$str[$i]."]<br />"; } } For instance, the string read from the file might contain CR LF characters. You could get rid of them using str_replace().
Thanks to Ravinder Reddy for the answer that was simple and worked for me: " trim the value for \t\n if($klasseinput == trim($student[$i][3])){ "
php - str_replace 2 parts with 2 different results
I want to add some html to a word based on the words in a compound word. For instance let's take the word "doghouse' Let's say $row2['word'] = 'doghouse'; $word = 'dog'; $otherword = 'house'; I want the end result to produce <span style="color:blue">dog</span><span style="color:red">house</span> My code so far that does not work (which I thought I could array the replace in str_replace but apparently I can't. $finalword = str_replace(array($word,$otherword),array("<span style='color:blue'>".$word."</span>","<span style='color:red'>".$word."</span>"),$row2['word']); Is str_replace the wrong choice for this?
str_replace is the correct function, but you're not really using it correctly. The way you've set it up, $row2 would have to be an array, but it's not, it's a string. So you need to use an alternate approach. Your code should look like this: str_replace($row2, $word.$otherword, "<span style='color:blue'>".$word."</span><span style='color:red'>".$otherword."</span>"); The first term ($row2) is where you're searching, what php calls the 'haystack'. The second term $word.$otherword is what you're searching for (the 'needle'). The third term is what you replace the 'needle' with when you find it.
I don't know how do you store your colors, but does it work for you? $row2['word'] = 'doghouse'; $words = array('dog','house'); $colors = array('blue', 'red'); $finalword = ""; for ($i = 0; $i < count($words); $i++) { $finalword .= "<span style='color:".$colors[$i]."'>".$words[$i]."</span>"; } echo $finalword;
Extracting meaningful data from this complicated string in PHP
I'm receiving some structured data for my PHP application, but the format is somewhat unpredictable and difficult to deal with. I don't get a say in the initial format of the data. What I get is a string (sample given below). [9484,'Víctor Valdés',8,[[['accurate_pass',[15]],['touches',[42]],['saves',[4]],['total_pass',[24]],['good_high_claim',[2]],['formation_place',[1]]]],1,'GK',1,0,0,'GK',31,183,78],[1320,'Carles Puyol',7.76,[[['accurate_pass',[50]],['touches',[75]],['aerial_won',[3]],['total_pass',[55]],['total_tackle',[1]],['formation_place',[6]]]],2,'DC',5,0,0,'D(CLR)',35,178,80],[5780,'Dani Alves',8.21,[[['accurate_pass',[58]],['touches',[99]],['total_scoring_att',[1]],['total_pass',[66]],['total_tackle',[6]],['aerial_lost',[1]],['fouls',[4]],['formation_place',[2]]]],2,'DR',22,0,0,'D(CR)',30,173,64],[83686,'Marc Bartra',8.31,[[['accurate_pass',[64]],['touches',[88]],['won_contest',[1]],['total_scoring_att',[1]],['aerial_won',[1]],['total_pass',[66]],['total_tackle',[5]],['aerial_lost',[1]],['fouls',[1]],['formation_place',[5]]]],2,'DC',15,0,0,'D(C)',22,181,70],[13471,'Adriano',6.72,[[['accurate_pass',[16]],['touches',[28]],['aerial_won',[2]],['total_pass',[18]],['total_tackle',[1]],['formation_place',[3]]]],2,'DL',21,1,31,'D(CLR),M(LR)',29,172,67] The above is data for 5 football players. This is what I need to get: [9484,'Víctor Valdés',8,[[['accurate_pass',[15]],['touches',[42]],['saves',[4]],['total_pass',[24]],['good_high_claim',[2]],['formation_place',[1]]]],1,'GK',1,0,0,'GK',31,183,78] [1320,'Carles Puyol',7.76,[[['accurate_pass',[50]],['touches',[75]],['aerial_won',[3]],['total_pass',[55]],['total_tackle',[1]],['formation_place',[6]]]],2,'DC',5,0,0,'D(CLR)',35,178,80] [5780,'Dani Alves',8.21,[[['accurate_pass',[58]],['touches',[99]],['total_scoring_att',[1]],['total_pass',[66]],['total_tackle',[6]],['aerial_lost',[1]],['fouls',[4]],['formation_place',[2]]]],2,'DR',22,0,0,'D(CR)',30,173,64] [83686,'Marc Bartra',8.31,[[['accurate_pass',[64]],['touches',[88]],['won_contest',[1]],['total_scoring_att',[1]],['aerial_won',[1]],['total_pass',[66]],['total_tackle',[5]],['aerial_lost',[1]],['fouls',[1]],['formation_place',[5]]]],2,'DC',15,0,0,'D(C)',22,181,70] [13471,'Adriano',6.72,[[['accurate_pass',[16]],['touches',[28]],['aerial_won',[2]],['total_pass',[18]],['total_tackle',[1]],['formation_place',[3]]]],2,'DL',21,1,31,'D(CLR),M(LR)',29,172,67] Now, what I've done manually in the above example I need to do reliably with PHP. As you see, each player has a set of data. In order to split the big string into individual players, I can't just explode it by "],[" because that substring appears within each player's data too an unpredictable number of times. Each player has a certain number of statistics (accurate_pass, touches etc) but they don't all have the same statistics. For instance, player #1 has "saves" and the others don't. Player #4 has "won_contest" and the others don't. There is no way to know who will have which stats. That means I can't just count commas until the new player or something similar. Each player has a number before his name, but that number has an unpredictable number of digits and there's no way to discern it from other numbers which may appear in the string. What I see as a constant occurrence for all players is the last bit: before the last closed bracket there are always 3 integers divided by commas. This type of substring (INT,INT,INT]) doesn't seem to appear in any other situation. Maybe this could be of some use?
A "hard" way to do this is parenthesis counting (less common in PHP, more common in text parsing languages)... <?php $str = "[9484,'Víctor Valdés',8,[[['accurate_pass',[15]],['touches',[42]],['saves',[4]],['total_pass',[24]],['good_high_claim',[2]],['formation_place',[1]]]],1,'GK',1,0,0,'GK',31,183,78],[1320,'Carles Puyol',7.76,[[['accurate_pass',[50]],['touches',[75]],['aerial_won',[3]],['total_pass',[55]],['total_tackle',[1]],['formation_place',[6]]]],2,'DC',5,0,0,'D(CLR)',35,178,80],[5780,'Dani Alves',8.21,[[['accurate_pass',[58]],['touches',[99]],['total_scoring_att',[1]],['total_pass',[66]],['total_tackle',[6]],['aerial_lost',[1]],['fouls',[4]],['formation_place',[2]]]],2,'DR',22,0,0,'D(CR)',30,173,64],[83686,'Marc Bartra',8.31,[[['accurate_pass',[64]],['touches',[88]],['won_contest',[1]],['total_scoring_att',[1]],['aerial_won',[1]],['total_pass',[66]],['total_tackle',[5]],['aerial_lost',[1]],['fouls',[1]],['formation_place',[5]]]],2,'DC',15,0,0,'D(C)',22,181,70],[13471,'Adriano',6.72,[[['accurate_pass',[16]],['touches',[28]],['aerial_won',[2]],['total_pass',[18]],['total_tackle',[1]],['formation_place',[3]]]],2,'DL',21,1,31,'D(CLR),M(LR)',29,172,67]"; $line = ','; $paren_count = 0; $lines = array(); for($i=0; $i<strlen($str); $i++) { $line.= $str{$i}; if($str{$i} == '[') $paren_count++; elseif($str{$i} == ']') { $paren_count--; if($paren_count == 0) { $lines[] = substr($line,1); $line = ''; } } } print_r($lines); ?>
Looks like #Boundless answer is correct, you can use json_decode, but you need to do a couple of things to the string you get first, which also seems like a valid json formatted string. This worked for me: <?php $str = "[9484,'Víctor Valdés',8,[[['accurate_pass',[15]],['touches',[42]],['saves',[4]],['total_pass',[24]],['good_high_claim',[2]],['formation_place',[1]]]],1,'GK',1,0,0,'GK',31,183,78],[1320,'Carles Puyol',7.76,[[['accurate_pass',[50]],['touches',[75]],['aerial_won',[3]],['total_pass',[55]],['total_tackle',[1]],['formation_place',[6]]]],2,'DC',5,0,0,'D(CLR)',35,178,80],[5780,'Dani Alves',8.21,[[['accurate_pass',[58]],['touches',[99]],['total_scoring_att',[1]],['total_pass',[66]],['total_tackle',[6]],['aerial_lost',[1]],['fouls',[4]],['formation_place',[2]]]],2,'DR',22,0,0,'D(CR)',30,173,64],[83686,'Marc Bartra',8.31,[[['accurate_pass',[64]],['touches',[88]],['won_contest',[1]],['total_scoring_att',[1]],['aerial_won',[1]],['total_pass',[66]],['total_tackle',[5]],['aerial_lost',[1]],['fouls',[1]],['formation_place',[5]]]],2,'DC',15,0,0,'D(C)',22,181,70],[13471,'Adriano',6.72,[[['accurate_pass',[16]],['touches',[28]],['aerial_won',[2]],['total_pass',[18]],['total_tackle',[1]],['formation_place',[3]]]],2,'DL',21,1,31,'D(CLR),M(LR)',29,172,67]"; $str = '[' . $str . ']'; $str = str_replace('\'','"', $str); //convert string to array $arr = json_decode($str); //now it's a php array so you can access any value //echo '<pre>'; //print_r( $arr ); //echo '</pre>'; echo $arr [0][1]; //prints "Victor Valdes" ?>
Your string looks like JSON but it is not valid JSON so json_decode() will not work. Your specific case could be converted to valid JSON by wrapping the string in a pair of [] and replacing the single quotes with double quotes: $string = str_replace("'", '"', $your_string); var_dump(json_decode('[' . $string . ']')); See this example. Of course the best solution would be to make sure that valid JSON is supplied because this will break easily if your text strings contain for example double quotes.
Try parsing as json, then pulling out what you want. Assuming that the data comes in blocks of 4 you can try: $arr = json_decode($str); for($i = 0; $i < count($arr) - 3; $i += 4) { $arr[] = new array($arr[$i], $arr[$i + 1], $arr[$i + 2], $arr[$i + 3]); }
Why not count the [ in a loop? Here's a quick untested loop that could get you started. $output = array(''); $brackets = 0; $index = 0; foreach (str_split($input) as $ch) { if ($ch == '[') { $brackets++; } $output[$index] .= $ch; if ($ch == ']') { $brackets--; if ($brackets === 0) { $index++; $output[$index] = ''; } } } Not very elegant though...
Why won't PHP recognize two equal strings?
I'm working on a php function that will compare the components of two arrays. Each value in the arrays are only one english word long. No spaces. No characters. Array #1: a list of the most commonly used words in the english language. $common_words_array Array #2: a user-generated sentence, converted to lowercase, stripped of punctuation, and exploded() using the space (" ") as a delimiter. $study_array There's also a $com_study array, which is used in this case to keep track of the order of commonly used words which get replaced in the $study_array by a "_" character. Using nested for loops, what SHOULD happen is that the script should compare each value in Array #2 to each value in Array #1. When it finds a match (aka. a commonly used english word), it will do some other magic that's irrelevant to the current problem. As of right now, PHP doesn't recognize when two array string values are equivalent. I'm adding in the code to the problematic function here for reference. I've added in a lot of unnecessary echo commands in order to localize the problem to the if statement. Can anybody see something that I've missed? The same algorithm worked perfectly in Python. function create_question($study_array, $com_study, $common_words_array) { for ($study=0; $study<count($study_array); $study++) { echo count($study_array)." total in study_array<br>"; echo "study is ".$study."<br>"; for ($common=0; $common<count($common_words_array); $common++) { echo count($common_words_array)." total in common_words_array<br>"; echo "common is ".$common."<br>"; echo "-----<br>"; echo $study_array[$study]." is the study list word<br>"; echo $common_words_array[$common]." is the common word<br>"; echo "-----<br>"; // The issue happens right here. if ($study_array[$study] == $common_words_array[$common]) { array_push($com_study, $study_array[$study]); $study_array[$study] = "_"; print_r($com_study); print_r($study_array); } } } $create_question_return_array = array(); $create_question_return_array[0] = $study_array; $create_question_return_array[1] = $com_study; return $create_question_return_array; } EDIT: At the suggestion of you amazing coders, I've updated the if statement to be much more simple for purposes of debugging. See below. Still having the same issue of not activating the if statement. if (strcmp($study_array[$study],$common_words_array[$common])==0) { echo "match found"; //array_push($com_study, $study_array[$study]); //$study_array[$study] = "_"; //print_r($com_study); //print_r($study_array); } EDIT: At bansi's request, here's the main interface snippet where I'm calling the function. $testarray = array(); $string = "This is a string"; $testarray = create_study_string_array($string); $testarray = create_question($testarray, $matching, $common_words_array); As for the result, I'm just getting a blank screen. I would expect to have the simplified echo statement output "match found" to the screen, but that's not happening.
(EDIT) make sure your that your splitting function removes excess whitespace (e.g. preg_split("\\s+", $input)) and that the input is normalized properly (lowercase'd, special chars stripped out, etc.).
On mobile and can't seem to copy text. You forgot a dollar sign when accessing the study array in your push command.
change array_push($com_study, $study_array[study]); to array_push($com_study, $study_array[$study]); // You missed a $ ^ here Edit: The following code outputs 3 'match found'. i don't know the values of $common_words_array and $matching, so i used some arbitrary values, also instead of using function create_study_string_array i just used explode. still confused, can't figure out what exactly you are trying to achieve. <?php $testarray = array (); $string = "this is a string"; $testarray = explode ( ' ', $string ); $common_words_array = array ( 'is', 'a', 'this' ); $matching = array ( 'a', 'and', 'this' ); $testarray = create_question ( $testarray, $matching, $common_words_array ); function create_question($study_array, $com_study, $common_words_array) { echo count ( $study_array ) . " total in study_array<br>"; echo count ( $common_words_array ) . " total in common_words_array<br>"; for($study = 0; $study < count ( $study_array ); $study ++) { // echo "study is " . $study . "<br>"; for($common = 0; $common < count ( $common_words_array ); $common ++) { // The issue happens right here. if (strcmp ( $study_array [$study], $common_words_array [$common] ) == 0) { echo "match found"; } } } $create_question_return_array = array (); $create_question_return_array [0] = $study_array; $create_question_return_array [1] = $com_study; return $create_question_return_array; } ?> Output: 4 total in study_array 3 total in common_words_array match foundmatch foundmatch found
Use === instead of == if ($study_array[$study] === $common_words_array[$common]) OR even better use strcmp if (strcmp($study_array[$study],$common_words_array[$common])==0)
Use built-in functions wherever possible to avoid unnecessary code and typos. Also, providing sample inputs would be helpful too. $study_array = array("a", "cat", "sat", "on","the","mat"); $common_words_array = array('the','a'); $matching_words = array(); foreach($study_array as $study_word_index=>$study_word){ if(in_array($study_word, $common_words_array)){ $matching_words[] = $study_word; $study_array[$study_word_index] = "_"; //do something with matching words } } print_r($study_array); print_r($matching_words);
PHP - Adding up an array of numbers?
I have a PHP script that returns an array of numbers: $fh = fopen ("./files/".$file, r); $filedata= explode('|', fgets($fh)); echo $filedata[5]; result: 0.23387718200684 3.6163940429688 0.030826568603516 Is there a way to get the script to return results added up as just one number?
echo array_sum($filedata); I think that should do the trick. Take a look here for further information. array_sum just sums up all the values in an array. EDIT The above example would work if your array was a flat array of integers, decimals etc. The following is how you would sum up a 2 dimensional array where the value you want to sum is at index position 5: var $sum = 0; foreach($filedata as $dataElement) { $sum += $dataElement[5]; } echo $sum; The above code assume that $filedata is an array of array's, each array in filedata has a value at index 5 that can be summed up... Is that more along the lines of what you need?
You could use array_sum: echo array_sum($filedata); Not working? As might be your case $filedata[5] actually contains a string with newlines and numbers. In this case you need some extra code. Important is the line endings - these can be different depending on your operating system / file. $sum = 0; $sLineEnding = "\r\n"; foreach($filedata as $data) { $parts = explode($sLineEnding, $data); $sum += array_sum($parts); } echo $sum;
As I see in your code, you are executing it in command line and you do echo $filedata[5] an you get different lines, this is because you are not paying attention to '\n' character, so what you need is to change that character somehow... there are different ways... In this example I'm changing the "\n" for " " simple space $fh = fopen ("./files/".$file, r); $filedata= explode('|', fgets($fh)); // if "\n" is not working try '\n' // One way echo str_replace(" ","\n",$filedata[5]); // Another way echo implode(" ", explode("\n", $filedata[5]));