Jump to content

[SOLVED] I cant figure why this is getting one value omitted from each index


Recommended Posts

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? :confused:

Can anyone see the bug in the code above thats doing this? Please someone! lol

Problem solved.  :D

 

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  :P

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.