PHP - Unexpected Behavior: Foreach... {$invert['country'][] = $value['country']}
I am trying to invert some arrays, making the x-values y, and the y-values x. I have found the code that works, but I cannot understand for the life of me,
I have a multidimensional $data array; that looks like this: Code: [Select] row:0 :: Country:Algeria row:1 :: Country:USA row:2 :: Country:Morocco The following code successfully extracts the appropriate 'column' from my array. Code: [Select] foreach($data as $value){ $invert['country'][] = $value['country']; } It returns: Code: [Select] $invert['country'] = Algeria, USA, Morocco But this makes no sense, it seems more logical that the code should be: Code: [Select] foreach($data as $value){ $invert[] ['country']= $value['country']; } Since we are accepting all values of the first array, and then specifying the second array. What am I missing here? Why is it working backwards? Similar TutorialsI have this code, its working pretty fine until i insert "echo "$ipDetail['country']";" to get the converted ip to name its origin Code: [Select] <?php $ip=$_SERVER['REMOTE_ADDR']; function countryCityFromIP($ipAddr) { //function to find country and city from IP address //Developed by Roshan Bhattarai http://roshanbh.com.np //verify the IP address for the ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : ""; $ipDetail=array(); //initialize a blank array //get the XML result from hostip.info $xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr); //get the city name inside the node <gml:name> and </gml:name> preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match); //assing the city name to the array $ipDetail['city']=$match[2]; //get the country name inside the node <countryName> and </countryName> preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches); //assign the country name to the $ipDetail array $ipDetail['country']=$matches[1]; //get the country name inside the node <countryName> and </countryName> preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match); $ipDetail['country_code']=$cc_match[1]; //assing the country code to array //return the array containing city, country and country code return $ipDetail; }$ipDetail="countryCityFromIP('12.215.42.19')"; echo "<b>IP Address= $ip</b> "; echo "$ipDetail['country']"; ?> this is the error Code: [Select] Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/GGME/public_html/addons/iptest.php on line 25 This script should detect what contry a visitor is from via there IP and display there contrys flag. However it does not work, please could you take a look and let me know if there are any obviouse errors as im stumped. Code: [Select] <?php $IPaddress=$_SERVER['REMOTE_ADDR']; $two_letter_country_code=iptocountry($IPaddress); Print "$IPaddress" include("IP_FILES/countries.php"); $three_letter_country_code=$countries[ $two_letter_country_code][0]; $country_name=$countries[$two_letter_country_code][1]; print "Two letters code: $two_letter_country_code<br>"; print "Three letters code: $three_letter_country_code<br>"; print "Country name: $country_name<br>"; // To display flag $file_to_check="flags/$two_letter_country_code.gif"; if (file_exists($file_to_check)){ print "<img src=flags/$file_to_check width=30 height=15><br>"; }else{ print "<img src=flags/noflag.gif width=30 height=15><br>"; } function iptocountry($ip) { $numbers = preg_split( "/\./", $ip); include("ip_files/".$numbers[0].".php"); $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]); foreach($ranges as $key => $value){ if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;} } } if ($two_letter_country_code==""){$two_letter_country_code="unkown";} return $two_letter_country_code; } ?> Thanks in Advance, Monk3h. Hi all Got a problem that seems simple, but I fail to solve it. Anybody who can help with country array? Right now the following code is active, which limits the available country to US only, for a certain language/area selection. It works fine. I would now only like to include an additional country (CA) as a choice besides US for the same language/area selection (without changing the language/area selection). If anyone knows the trick pls help. Existing Code:
if( !function_exists('filter_woocommerce_countries_shipping_countries') ) {
I have the product catalog/shopping cart. I need to modify the registration so that it allows the customer to complete the form but then takes him to a message telling him who to contact based on the country he enters during registration. Maybe an if country=A go to... If country=B go to... else go to... Okay we are using a javascript, but the javascript that the site comes from has bans all irans ips... and it loads slow for those people, how would I place a if statement saying dont load this javascript if in Iran, thanks... I know how to do it, but I can't find a real set number, and I would use just 1 ip but the guy has dynamic... thanks ahead of time This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=353007.0 Hello I am trying to show/geo target content for certian countries that my visitors to my site are from. I have doing a little research on google and have found a few script but for some reason they do not work. I know about the geoip database from maxmind but I am looking for a remote solution as the geoip database is said to be a heavy strain on servers if you get a lot of visitors. I am hosted on a shared server so I do not want to get my account suspended for over working the server. Also I have a few sites that will use this idea. This is a script I found whilst googling and all it does is give me an error. Code: [Select] <?php $ip = $_SERVER [ 'REMOTE_ADDR' ]; $country = file_get_contents ( 'http://api.hostip.info/country.php?ip=' . $ip ); if ( $country == "US" ) { echo "<American-English Ringtone Ad Tag>" ; } elseif ( $country == "AU" OR $country == "UK" OR $country == "IE" ) { echo "<British-English Ringtone Ad Tag>" ; } else { echo "<YPN Ad Tag>" ; } ?> This is the error it gives me:- Quote Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/username/public_html/webaddress.co.uk/test/test.php on line 7 What I am trying to do is to geo target Javascript ads for different countries. When I add the javascript ad it gives me that error. If I just put in normal text then it works ok. Thank you all for your help in advance. same with trivago Welcome to www.trivago.com. trivago is available locally: Continue to trivago Australia or Stay on www.trivago.com Hello from the UK! We want to make sure you get to the right place, and noticed you appear to be visiting us from Australia. If you would prefer to visit our Australian site, please just follow the link below. Go to our Australian site comparethemarket.com.auhi professionals I was not sure where to exactly post this as I am not to sure if it is a php question. We are developing a new website for another country and are looking to geo target people with the country in mind for instance if I visit comparethemarket .com or comparethemarket uk from here in Australia a pop up window appears saying the following I'm trying to block access to a particular page if the visitor is not from a specific country (using Maxmind Geoip Country database), but not quite sure how to code this properly: I tried this and many variations of this inside the head tags, bud doesn't work. Any suggestions on the best way to go about this? Code: [Select] <?php require_once("geoip.inc"); $gi = geoip_open("/home/username/GeoIP.dat",GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); if($country_code !== 'US') header("HTTP/1.0 404 Not Found"); exit; ?> This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=359083.0 hey guys just wondering what u think the best way to do this.....on my form registration for users....I have a drop down for countrys....but I dont want to have 2 drop downs 1 for state and 1 for province if they choose canada/usa....I have seen alot of java scrit on such sites where if you choose Canada for country, then it switches to a drop down for just provinces, not states. Same for zip code/postal code...american zip code would be like 90210 where as a postal code would be like n5j2S1. Is there a script or something already I can use with all the countrys/provinces/states etc already entered so im not re-inventing the wheel here? If someone can point me in right directon id appreciate it. Thanks This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=345840.0 Hi, I have a search form where users can search by age and country. Users can also save their search so they can go back and do it again but the problem is when they reload their saved search, I need it to select the country that has been saved in the drop down. So if someone searched the United Kingdom, I need it to show United Kingdom in the select drop down instead of (Select Country). Is there any easy and quick way around this? Many Thanks I am trying to force the "default" behavior in the Switch statement below. Why does having a "0" trigger the first Switch statement and not the last one?? switch (0){ /* switch ($resultsCode){ */ // Insert Succeeded. case 'ACCOUNT_MEMBER_ACCT_CREATED': echo '<h1>Member Account Created</h1>'; echo '<p>Congratulations!</p> <p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p> <p>Please click on the link in that e-mail to activate your account.</p>'; break; // Insert Failed. case 'ACCOUNT_MEMBER_ACCT_FAILED': echo '<h1>Account Creation Failed</h1>'; echo '<p>You could not be registered due to a system error.</p>'; echo '<p>Please contact the System Administrator.</p>'; break; // Default Error. default: echo '<h1>Error</h1>'; echo '<p>Oops! A system error has occurred. Please try again.</p>'; echo '<ul class="button2"> <li> <a class="testButton" href="' . BASE_URL . 'members/create_account.php">Create Account</a> </li> </ul>'; break; } Debbie My foreach loop is skipping an element?
How is this possible?
echo '<p>-- Begining of Vardump --</p> '; var_dump($EventTotals); echo ' <p>-- End of Vardump --</p> <p>-- Begining of Just echo --</p> <p>'.$EventTotals[1]['TITLE'].'</p> <p>'.$EventTotals[2]['TITLE'].'</p> <p>-- End of Just echo --</p> <p>-- Begining of Foreach --</p> '; foreach ($EventTotals as $DATA); { echo '<p>'.$DATA['TITLE']."</p>\n"; } echo '<p>-- End of Foreach --</p> ';Gives me this output: <p>-- Begining of Vardump --</p> array(2) { [2]=> array(3) { ["TITLE"]=> string(9) "Book Sale" ["TARGETHOURS"]=> string(4) "0.00" ["HOURS"]=> float(4) } [1]=> array(3) { ["TITLE"]=> string(15) "Spring Jogathon" ["TARGETHOURS"]=> string(5) "10.00" ["HOURS"]=> float(7) } } <p>-- End of Vardump --</p> <p>-- Begining of Just echo --</p> <p>Spring Jogathon</p> <p>Book Sale</p> <p>-- End of Just echo --</p> <p>-- Begining of Foreach --</p> <p>Spring Jogathon</p> <p>-- End of Foreach --</p>Why does the foreach skip one of the elements of the array? Bug or am I missing something obvious? Edited by Firemankurt, 04 October 2014 - 04:36 PM. Hi, I'm trying to invert a random section of an image. The only way I've thought of doing this in PHP, is to split the image up into sections using imagecopyresampled then negate one of the sections using the imagefilter IMG_FILTER_NEGATE. Then put them all back together using imagecopymerge. Now I have managed to get this working when negating the first section ($split_first), but whenever I try to negate any other section (e.g. $split_sec) nothing happens. Now I know my code doesn't randomly invert it, I'm just trying to get it to work for each section first then I'll try get that working. Below I have posted my code. Any help would be be appreciated, Thank you in advance! <?php $image=imagecreate(400, 100); $red = imagecolorallocate($image, 255, 0, 0); imagefill($image, 0, 0, $red); $split_first=imagecreate(100, 100); $split_sec=imagecreate(100, 100); $split_thir=imagecreate(100, 100); $split_four=imagecreate(100, 100); imagecopyresampled($split_first,$image,0,0,0, 0,100,100,100,100); imagecopyresampled($split_sec,$image,100,0,100,0,100,100,100,100); imagecopyresampled($split_thir,$image,200,0,200,0,100,100,100,100); imagecopyresampled($split_four,$image,300,0,300,0,100,100,100,100); imagefilter($split_sec, IMG_FILTER_NEGATE); imagecopymerge($image,$split_first,0,0,0,0,100,100,100); imagecopymerge($image,$split_sec,100,0,100,0,0,100,100); imagecopymerge($image,$split_thir,200,0,200,0,0,100,100); imagecopymerge($image,$split_thir,300,0,300,0,0,100,100); imagepng($image); imagedestroy($image); header('Content-Type: image/png'); echo $image; ?> This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355772.0 This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=353396.0 |