Meep3D Posted January 20, 2007 Share Posted January 20, 2007 I am currently writing a parser that will read in a data file and convert it to a PHP array. It works great, except I am getting constant null values littered through the array, and I don't see how they got in there in the first place, let alone how to get rid of them (which would be fine). They seem to occur when joining arrays, but I just can't pin down where! :(They basically look like "[] => ". If anyone knows where they come from, or how to get rid of them, I would be appreciative! I'll post the full code if you think it'll help - I am hoping it's a common easy fix/mistake though.[code]Array( [Zandalar Tribe] => Array ( [Value] => 0:3000 [AtWar] => 0 [Standing] => Neutral ) [] => [Darkmoon Faire] => Array ( [Value] => 55:3000 [AtWar] => 0 [Standing] => Neutral ))[/code] Link to comment https://forums.phpfreaks.com/topic/35002-null-array-values-appearing/ Share on other sites More sharing options...
.josh Posted January 20, 2007 Share Posted January 20, 2007 ...and a print_r of your array is supposed to help us out...how? What happened to the part where you said you were going to post some actual code? Link to comment https://forums.phpfreaks.com/topic/35002-null-array-values-appearing/#findComment-165087 Share on other sites More sharing options...
Meep3D Posted January 20, 2007 Author Share Posted January 20, 2007 [quote author=Crayon Violent link=topic=123271.msg509245#msg509245 date=1169321748]...and a print_r of your array is supposed to help us out...how? What happened to the part where you said you were going to post some actual code?[/quote]I thought it may be a common issue. I can't even remove them from the array afterwards. It calls itself recursively and adds the result onto the current wokring array until a full one is built and passed back by the original instance of the program. I think the problem is somewhere around that area.Here's the code. matchBrace () returns the position of the matching { } brace so I can filter out blocks. Apologies for the lack of comments and code quality.[code] /* Read & Parse bank data string - Pass text, returns array. */ function parseStatement ( $statement ) { $literalString = false; $addChar = true; $doubleDash = false; $singleDash = false; $currentString = ''; $variableString = ''; $valueString = ''; $arrayData = array (); $arrayCount = 0; $braceArray = array (); for ( $counter = 0; $counter < strlen ( $statement ); $counter ++ ) { $char = substr ( $statement, $counter, 1 ); $addChar = true; # # Avoid literal strings. # if ( $char == '"' ) { $addChar = false; $literalString = !$literalString; } if ( $char == '-' && !$literalString ) { $addChar = false; if ( $singleDash == true ) { $doubleDash = true; $singleDash = false; } else { $singleDash = true; } } if ( $char == '[' && !$literalString ) { $addChar = false; $squareQuote = true; } if ( $char == ']' && !$literalString ) { $addChar = false; $squareQuote = false; if ( $doubleDash ) { $currentString = ''; $doubleDash = false; } } if ( $char == '=' && !$literalString ) { $addChar = false; $variableString = trim ( $currentString ); $currentString = ''; } if ( $char == ',' && !$literalString ) { $addChar = false; $arrayData[$variableString] = trim ( $currentString ); $variableString = ''; $currentString = ''; } if ( $char == '{' && !$literalString ) { $endCounter = $this->matchBrace ( $statement, $counter + 1 ); $length = $endCounter - $counter; $braceString = substr ( $statement, $counter + 1, $length - 1 ); # echo "<p>Brace $variableString ( $arrayCount ) = ( $counter to $endCounter = $length ): $braceString</p>"; $counter = $endCounter; $braceArray = $this->parseStatement ( $braceString ); # Check if there is a name for the array/element if ( $variableString ) { echo "<pre><hr/>"; print_r ( $braceArray ); $arrayData[$variableString] = $braceArray; print_r ( $arrayData ); $variableString = ''; } else { $arrayData[$arrayCount] = $braceArray; $arrayCount++; } $addChar = false; } if ( $char == '}' && !$literalString ) { $addChar = false; } if ( $addChar ) $currentString .= $char; } return $arrayData; }[/code] Link to comment https://forums.phpfreaks.com/topic/35002-null-array-values-appearing/#findComment-165089 Share on other sites More sharing options...
kenrbnsn Posted January 20, 2007 Share Posted January 20, 2007 What does the input data look like? Please post a sample.Ken Link to comment https://forums.phpfreaks.com/topic/35002-null-array-values-appearing/#findComment-165096 Share on other sites More sharing options...
Meep3D Posted January 20, 2007 Author Share Posted January 20, 2007 Here's the top bit. It's quite a large file but this is enough to reproduce the error...[code]rpgoCPpref = { ["enabled"] = true, ["debug"] = false, ["questsfull"] = false, ["tooltipshtml"] = true, ["tooltip"] = true, ["lite"] = true, ["scan"] = { ["inventory"] = true, ["talents"] = false, ["honor"] = true, ["reputation"] = true, ["spells"] = false, ["pet"] = false, ["equipment"] = true, ["professions"] = false, ["mail"] = false, ["skills"] = true, ["quests"] = false, ["bank"] = true, }, ["ver"] = 20001, ["fixicon"] = true, ["talentsfull"] = true, ["fixtooltip"] = true, ["fixcolor"] = true, ["button"] = true, ["reagentfull"] = true, ["fixquantity"] = true,}[/code]The full code for the entire class is here...[code]<?php class scanStatement { var $items = array(); function scanStatement ( $statementFile ) { $statement = $this->fileContents ( $statementFile ); for ( $counter = 0; $counter < 30; $counter++ ) { # Trim off exciting characters. $statement = str_replace ( chr($counter), "", $statement ); } $this->items = $this->parseStatement ( $statement ); } /* Read & Parse bank data string - Pass text, returns array. */ function parseStatement ( $statement ) { $literalString = false; $addChar = true; $doubleDash = false; $singleDash = false; $currentString = ''; $variableString = ''; $valueString = ''; $arrayData = array (); $arrayCount = 0; $braceArray = array (); for ( $counter = 0; $counter < strlen ( $statement ); $counter ++ ) { $char = substr ( $statement, $counter, 1 ); $addChar = true; # # Avoid literal strings. # if ( $char == '"' ) { $addChar = false; $literalString = !$literalString; } if ( $char == '-' && !$literalString ) { $addChar = false; if ( $singleDash == true ) { $doubleDash = true; $singleDash = false; } else { $singleDash = true; } } if ( $char == '[' && !$literalString ) { $addChar = false; $squareQuote = true; } if ( $char == ']' && !$literalString ) { $addChar = false; $squareQuote = false; if ( $doubleDash ) { $currentString = ''; $doubleDash = false; } } if ( $char == '=' && !$literalString ) { $addChar = false; $variableString = trim ( $currentString ); $currentString = ''; } if ( $char == ',' && !$literalString ) { $addChar = false; $arrayData[$variableString] = trim ( $currentString ); $variableString = ''; $currentString = ''; } if ( $char == '{' && !$literalString ) { $endCounter = $this->matchBrace ( $statement, $counter + 1 ); $length = $endCounter - $counter; $braceString = substr ( $statement, $counter + 1, $length - 1 ); $counter = $endCounter; $braceArray = $this->parseStatement ( $braceString ); # Check if there is a name for the array/element if ( $variableString ) { $arrayData[$variableString] = $braceArray; $variableString = ''; } else { $arrayData[$arrayCount] = $braceArray; $arrayCount++; } $addChar = false; } if ( $char == '}' && !$literalString ) { $addChar = false; } if ( $addChar ) $currentString .= $char; } return $arrayData; } /* Returns matching brace position in string */ function matchBrace ( $string, $startpos = 1, $type = '{}' ) { $startChar = substr ( $type, 0, 1 ); $endChar = substr ( $type, 1, 1 ); $literalString = false; $endCount = 0; $indentLevel = 1; for ( $counter = $startpos; $counter < strlen ( $string ); $counter++ ) { $char = substr ( $string, $counter, 1 ); $readMode = true; # Parse single quotes. if ( $char == '"' ) { $literalString = !$literalString; $readMode = true; } # Increase Indent if ( !$literalString && ( $char == $startChar ) ) $indentLevel++; # Decrease Indent if ( !$literalString && ( $char == $endChar ) ) $indentLevel--; # Check if to drop out of loop if ( $indentLevel < 1 ) { $endCount = $counter; break; } } if ( $endCount == 0 ) { echo "0 endCount in matchBrace. Correcting."; $endCount = $startpos + strlen ( $string ); } return $endCount; } /* Return contents of a file */ function fileContents ( $filename ) { $file = fopen ( $filename, "r" ); $data = fread ( $file, filesize ( $filename ) ); fclose ( $file ); return ( $data ); } }?>[/code]You just need to pass it the directory/filename that the input is in, and then read the $this->items array when its done.thx. Link to comment https://forums.phpfreaks.com/topic/35002-null-array-values-appearing/#findComment-165100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.