keldorn Posted October 23, 2009 Share Posted October 23, 2009 I'm generating this array by giving this code below a Mysql Result from mysql_fetch_assoc. Mysql Returns a result that looks like this. [159] => Array ( [id] => 3203 [country] => DE [short_url] => example.net ) [160] => Array ( [id] => 3204 [country] => US [short_url] => example.com ) This is my code: $cw = array('ad'=>'Andorra', 'ae'=>'United Arab Emirates', 'af'=>'Afghanistan', 'ag'=>'Antigua and Barbuda', 'ai'=>'Anguilla', 'al'=>'Albania', 'am'=>'Armenia', 'an'=>'Netherlands Antilles', 'ao'=>'Angola', 'aq'=>'Antartica', 'ar'=>'Argentina', 'as'=>'American Samoa', 'at'=>'Austria', 'aw'=>'Aruba', 'ax'=>'Aland Islands', 'az'=>'Azerbaijan', 'ba'=>'Bosnia and Herzegovina', 'bb'=>'Barbados', 'bd'=>'Bangladesh', 'be'=>'Belgium', 'bf'=>'Burkina Faso', 'bg'=>'Bulgaria', [.... and continues threw all the country codes...]); // Contains Array of countries, 2 letter code => Full name. $cw2 = array_flip($cw); $sorted = array(); foreach($result as $link){ $country = strtolower($link['country']); if(in_array($country,$cw2)){ if(array_key_exists($country,$sorted)){ $arr[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array_merge_recursive($arr[$country],$sorted[$country]); } else { $sorted[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array('country'=>$country); } } } This will generate an array looks like this. Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => us ) [ca] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => ca ) [de] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => de ) However, I noticed that this is omitting 1 value for each country index! For example in the Mysql array thats returned say I have, 2 results that have the country code "us", You think this would be generated from the code above. Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [1] => <a href="url.php?id=3">somelink</a><br/> [country] => us ) But that is not what is happening. This is what is happening Array ( [us] => Array ( [0] => <a href="url.php?id=1">somelink</a><br/> [country] => us ) Lastly if there is one value returned from mysql with that country code I get this. [de] => Array ( [country] => de ) Completly blank! But there was 1 result, it should of put it in there right? Can anyone see the bug in the code above thats doing this? Please someone! lol Quote Link to comment https://forums.phpfreaks.com/topic/178676-solved-i-cant-figure-why-this-is-getting-one-value-omitted-from-each-index/ Share on other sites More sharing options...
keldorn Posted October 23, 2009 Author Share Posted October 23, 2009 Problem solved. For some reason this was cuasing the bug. $sorted[$country] = array("<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); $sorted[$country] = array('country'=>$country); <-- Point of bug Changed to this. $sorted[$country] = array('country'=>$country,"<a href=\"url.php?id={$link['id']}\">{$link['short_url']}</a><br/>"); I'm guessing what was happening, was when it hit. $sorted[$country] = array('country'=>$country); It unset the array index for that country, and then when it looped again (if). Then it skipped that and fill out the index normally. btw I notice you guys never reply to threads that invole complicated multidemisional arrays. Are they too complicated for you guys? LOL Quote Link to comment https://forums.phpfreaks.com/topic/178676-solved-i-cant-figure-why-this-is-getting-one-value-omitted-from-each-index/#findComment-942514 Share on other sites More sharing options...
Andy-H Posted October 23, 2009 Share Posted October 23, 2009 It is because you were overwriting the array as you were inputting the same key, like you said. You could do it this way, which I find is more readable and easier to expand. $sorted[$country][] = '<a href="url.php?id=' . $link['id'] . '">' . $link['short_url'] . '</a><br/>'; $sorted[$country]['country'] = $country; //edit removed whitespace, cant stand it when assignment operators aren't lined up lol Quote Link to comment https://forums.phpfreaks.com/topic/178676-solved-i-cant-figure-why-this-is-getting-one-value-omitted-from-each-index/#findComment-942520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.