2 Replies - 141 Views - Last Post: 04 February 2012 - 09:28 AM Rate Topic: -----

Topic Sponsor:

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Long winded approach for adding variables into an array in PHP

Posted 04 February 2012 - 09:10 AM

Background

I've created a website to display weather and currency rates for two cites; London and New York. The data comes from XML feeds from Yahoo Weather and the Money Converter.

Solution 1

I have a single configuration file to store variables and arrays for both cities.

My current solution is the following for my configuration file cityConfig.php:

<?php

// Define arrays // 
$cities = array();
$currencyRateHeadings = array(); 
$currencySource = array(); 
$weatherSource = array(); 

// Feed URL's //
$yahooWeather = 'http://weather.yahooapis.com/forecastrss?p=';
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';

// City 1 //
$city = 'London';
$currencyRateHeading = '1 British Pound Sterling';
$cityWeatherSourceCode = 'UKXX0085';
$cityWeatherSource = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode = 'GBP';
$cityCurrencySource = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';

array_push($currencySource, $cityCurrencySource);
array_push($weatherSource, $cityWeatherSource);
array_push($cities, $city);
array_push($currencyRateHeadings, $currencyRateHeading);

// City 2 //
$city = 'New York';
$currencyRateHeading = '1 US Dollar';
$cityWeatherSourceCode = 'USNY0996';
$cityWeatherSource = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode = 'USD';
$cityCurrencySource = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';

array_push($cities, $city);
array_push($currencyRateHeadings, $currencyRateHeading);
array_push($currencySource, $cityCurrencySource);
array_push($weatherSource, $cityWeatherSource);

?>



Solution 2

Another alternative I come up with was to rename the variables for each city and adding them all to an array at the end of this script but this means declaring 'poor' variable names like:

// City 1 //
$city1 = 'London';
$currencyRateHeading1 = '1 British Pound Sterling';



Basically adding a 1 at the end of the variable name to define city 1 or city 2. Any recommendations for optimising this file and placing values into an array? I'm trying to develop a better understanding.

I don't really want to go down the road of using databases like MySQL.

I call the arrays in my threeColumnContainer.php script depending on which page the user is viewing by setting a $current variable.

<li class="leftCol">
    <p>
        <?php
        switch ($currentPage) {
            case 'index.php':
                echo displayCityContent($weatherSource[0], $columnSubheading);
                break;
            case 'currencyRate.php':
                echo displayCurrencyRateContent($currencySource[0]);
                break;
            default:
                content_unavailable();
                break;
        }
        ?>
    </p>
</li>
<li class="rightCol">
    <p>
        <?php
        switch ($currentPage) {
            case 'index.php':
                echo displayCityContent($weatherSource[1], $columnSubheading);
                break;
            case 'currencyRate.php':
                echo displayCurrencyRateContent($currencySource[1]);
                break;
            default:
                content_unavailable();
                break;
        }
        ?>                        
    </p>
</li>


Is This A Good Question/Topic? 0
  • +

Replies To: Long winded approach for adding variables into an array in PHP

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 378
  • View blog
  • Posts: 740
  • Joined: 17-June 08

Re: Long winded approach for adding variables into an array in PHP

Posted 04 February 2012 - 09:28 AM

I'm confused. You seem to be asking whether it's better to use different variables or re-use the same ones. But, honestly, why would you bother with the temporary variables at all? Why not just add your values straight to the arrays? There really isn't any need for the temporary variables at all.
$cities[] = 'London';
$currencyRateHeadings[] = '1 British Pound Sterling';
$wWeatherSource[] = $yahooWeather . $cityWeatherSourceCode . '&u=c';
// ... etc. ...



And while I'm at it, I would be remiss if I didn't mention that using parallel arrays like this is a very poor way to structure your data and is to be avoided if at all possible. For data like this, objects would be more appropriate, or maybe put all the data for one city in a single associative array. Something where all the data for one location is together.
Was This Post Helpful? 2
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Long winded approach for adding variables into an array in PHP

Posted 04 February 2012 - 09:28 AM

I'd put the data in a MySQL database, but assuming that for some reason you can't do that, you could use a flat file:
<?php
$cities = file("Weathercities.txt");
foreach($cities as $city) {
	$weather[] = explode("|", $city);
}
for($row = 0; $row < count($weather); $row++) {
	for($col = 0; $col < count($weather[$row]); $col++) {
		echo $weather[$row][$col]." - ";
	}
	echo "<br>";
}
?>

Weathercities.txt
New York|US Dollar|USNY0996|USD
London|British Pound Sterling|UKXX0085|GBP

This post has been edited by CTphpnwb: 04 February 2012 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1