Jump to content

I am trying to get a value from this Associative array.


theITvideos

Recommended Posts

Hi guys.

 

I have an associative array called $VisitorCountryID

 

$VisitorCountryID = filter_countries(array('Country' => $_SESSION['UserCountryName'])); 

 

and the function filter_countries() returns all the information relating to the visitors country from the database.

 

To find out what we have in the array, we do:

 

print_r($VisitorCountryID); 

 

and it gives us:

 

Array ( [14] => Array ( [CountryId] => 14 [Country] => Australia [FIPS104] => AS [iSO2] => AU [iSO3] => AUS [iSON] => 36 [internet] => AU [Capital] => Canberra [Comment] => ISO includes Ashmore and Cartier Islands,Coral Sea Islands [sortOrder] => 1 ) ) 

 

Which is OK. Now i just want the CountryId which you can see in the array is 14 therefore I write:

 

print_r($VisitorCountryID['CountryId']);

 

Which should give me 14 but it return blank no output.

 

I am surprised. What do you think is missing here.

 

Any response / feedback always welcomed!

 

Cheers!

Link to comment
Share on other sites

Hi

 

You have an array containing an array. So what you would need would be print_r($VisitorCountryID[14]['CountryId']); which is fairly pointless if you do not know that it is country 14.

 

The issue would appear to be that the function filter_countries() is returning more than is required. Can you post the code for that function.

 

All the best

 

Keith

Link to comment
Share on other sites

It sounds like you're not building the array right.  The first index should be 0 not 14.

something like.

Array ( [0] => Array ( [CountryId] => 14 [Country] => Australia [FIPS104] => AS [iSO2] => AU [iSO3] => AUS [iSON] => 36 [internet] => AU [Capital] => Canberra [Comment] => ISO includes Ashmore and Cartier Islands,Coral Sea Islands [sortOrder] => 1 ) ) 

 

so you could do this

echo $VisitorCountryID[0]['CountryID']; 

that way if you're returning more than one result you could also do this

foreach($VisitorCountryID as $country)
{
echo $country['CountryID']; 
}

Link to comment
Share on other sites

Ok the definition for the code is here:

 

By the way.. just for the info, this function would normally accept Country ID and returns the country info. Since we don't have the country ID, we just have the visitors country name which we get through a 3rd party script.

 

And to my surprise, this function does return the country info upon passing the country name as you saw in the print_r() output i.e.

 

Array ( [14] => Array ( [CountryId] => 14 [Country] => Australia [FIPS104] => AS [iSO2] => AU [iSO3] => AUS [iSON] => 36 [internet] => AU [Capital] => Canberra [MapReference] => Oceania [NationalitySingular] => Australian [NationalityPlural] => Australians [Currency] => Australian dollar [CurrencyCode] => AUD [Population] => 19357594 [Title] => Australia [measurement] => metric [Comment] => ISO includes Ashmore and Cartier Islands,Coral Sea Islands [sortOrder] => 1 ) ) 

 

Anyways, here is the filter_country() function definition. it is little intimidating  ;) and here it is:

 

function filter_countries($args=array())
{
global $connection;
global $GLOBAL_DATABASE;
$selectFields = (is_null($args['selectFields'])) ? ' * ' : ((is_array($args['selectFields'])) ? implode(',', $args['selectFields']) : "$args[selectFields]");
$limit = (isset($args['limit'])) ? " LIMIT $args[limit] " : ' ';
$order = (isset($args['orderby'])) ? " ORDER BY $args[orderby] " : ' ';
$unique = array('CountryId');

$fields = array( 'FIPS104', 'ISO2', 'ISO3', 'ISON', 'Internet', 'Capital', 'MapReference', 'NationalitySingular', 'NationalityPlural', 'Currency', 'CurrencyCode', 'Title', 'Comment');
foreach ($fields as $f)
if (isset($args[$f]))
$sql .= " AND `$f` LIKE '%$args[$f]%' ";

$exactfields = array('Country', 'CountryId');
foreach ($exactfields as $f)
{
	if (isset($args[$f . ' ne']))
	$sql .= " AND `$f` != '" . $args[$f . ' ne'] . "' ";
	if (isset($args[$f . ' lt']))
	$sql .= " AND `$f` < '" . $args[$f . ' lt'] . "' ";
	if (isset($args[$f . ' lte']))
	$sql .= " AND `$f` <= '" . $args[$f . ' lte'] . "' ";
	if (isset($args[$f . ' gt']))
	$sql .= " AND `$f` > '" . $args[$f . ' gt'] . "' ";
	if (isset($args[$f . ' gte']))
	$sql .= " AND `$f` >= '" . $args[$f . ' gte'] . "' ";
	if (isset($args[$f]))
	$sql .= " AND `$f` = '" . $args[$f] . "' ";
}

$numberfields = array( 'Population', 'sortOrder');
foreach ($numberfields as $f)
{
	if (isset($args[$f . ' ne']))
	$sql .= " AND `$f` != '" . $args[$f . ' ne'] . "' ";
	if (isset($args[$f . ' lt']))
	$sql .= " AND `$f` < '" . $args[$f . ' lt'] . "' ";
	if (isset($args[$f . ' lte']))
	$sql .= " AND `$f` <= '" . $args[$f . ' lte'] . "' ";
	if (isset($args[$f . ' gt']))
	$sql .= " AND `$f` > '" . $args[$f . ' gt'] . "' ";
	if (isset($args[$f . ' gte']))
	$sql .= " AND `$f` >= '" . $args[$f . ' gte'] . "' ";
	if (isset($args[$f]))
	$sql .= " AND `$f` = '" . $args[$f] . "' ";
}
$datefields = array();
foreach ($datefields as $f)
{
	if (isset($args[$f . ' ne']))
	$sql .= " AND `$f` != '" . $args[$f . ' ne'] . "' ";
	if (isset($args[$f . ' lt']))
	$sql .= " AND `$f` < '" . $args[$f . ' lt'] . "' ";
	if (isset($args[$f . ' lte']))
	$sql .= " AND `$f` <= '" . $args[$f . ' lte'] . "' ";
	if (isset($args[$f . ' gt']))
	$sql .= " AND `$f` > '" . $args[$f . ' gt'] . "' ";
	if (isset($args[$f . ' gte']))
	$sql .= " AND `$f` >= '" . $args[$f . ' gte'] . "' ";
	if (isset($args[$f]))
	$sql .= " AND `$f` LIKE '%" . $args[$f] . "%' ";
}

$select = "select $selectFields from `$GLOBAL_DATABASE`.`Countries` WHERE 1 $sql $order $limit";

if ($args['verbose'] == true)
echo $select;
if (!$result = mysql_query($select, $connection))
return false;
$return = array();
while ($row = mysql_fetch_assoc($result))
{
	if (file_exists(BASE_DIR . "images/flags/" . strtolower($row['ISO2']) . ".gif"))
	{
		$row['imageLink'] = "/images/flags/" . strtolower($row['ISO2']) . ".gif";
	}
	$return[$row['CountryId']] = $row;
}
if ($args['limit'] == 1 || check_if_exists($args, $unique))
return current($return);
return $return;
}

 

And we call or use this function by passing the country Name (because thats what we have) in order to get the country Id:

 

$VisitorCountryID = filter_countries(array('Country' => $_SESSION['UserCountryName']));

 

Waiting for your response :)

 

What change can we make inside the function to get proper info back?

 

 

Link to comment
Share on other sites

This will more than likely mess something else up but... here is the problem

$return[$row['CountryId']] = $row;

 

So in order to get the countryid from returned array you would need to already know the countryid in which case you wouldn't need the countryid.

 

This

$return[]=$row;

Then

echo $VisitorCountryID[0]['CountryID']; 

would fix this problem. But if this function is used anywhere else it would break that.

 

Link to comment
Share on other sites

Hi

 

Not sure I would use foreach.

 

If you keep the function the same then maybe:-

 

$VisitorCountryIdKeys = array_keys($VisitorCountryID);
print_r($VisitorCountryID[$VisitorCountryIdKeys[0]]);

 

$CurVisitorCountryId = array_pop($VisitorCountryID);
print_r($CurVisitorCountryId);

 

$VisitorCountryIdKeys = array_keys($VisitorCountryID);
print_r(current($VisitorCountryID));

 

Not tested any of them but they should work.

 

All the best

 

Keith

Link to comment
Share on other sites

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.