Jump to content

Recommended Posts

First: i'm pretty new to PHP.

Second, the situation: i've got a array of universities. Each university within the array, which is also an array has values like an ID and Name. I wan't to add a value to that university from a xml feed, a URL. I can match ID's to find the corresponding university in the xml feed.

Third, my question: Why do i lose the ['hURL'] outside of my foreach loop(s) ? It's working inside the foreach loop ... 

Fourth, here's the code:

 


$hDirectory = get_xml_feed('http://myawesomefeed.xml');

$universiteiten = array(
        $uniUtrecht = array('uniNaam' =>'Universiteit Utrecht', 'uniID' => 'uu'),
        $uniWageningen = array('uniNaam' =>'Wageningen University', 'uniID' => 'wur')
);


foreach( $universiteiten as $universiteit ) {

echo '<strong>' . $universiteit['uniNaam'] . ':' . '</strong> <br />';	


foreach( $hDirectory->Entity as $school ) { 

	if ( $universiteit['uniID'] == $school->orgUnitId ) {

		$hURL = (string) $school->DirectoryURL;

		$universiteit['uniURL'] = $hURL; 
		echo  'Universiteit ID: ' . $universiteit['uniID'] . '<br />';
		echo 'Hodex URL: ' .  $universiteit['hURL'] . '<br /><br />';	//this works just fine

	}

}	

}

echo '<br/>';
print_r_html($uniUtrecht); // but here the [hURL] is missing


Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/
Share on other sites

$universiteit is actually a copy, not the original. When you add the hURL you're only adding it to the copy.

 

You could use references but they can backfire if you don't know exactly how it all works. So use the array's keys and update $universiteiten:

foreach( $universiteiten as $universiteitkey => $universiteit ) {

$universiteiten[$universiteitkey]['uniURL'] = $hURL;

echo 'Hodex URL: ' .  $hURL . '

'; // why bother with the array when you have the value?

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1323939
Share on other sites

Thanks requinix.

Within the foreach loop it works ! :) But when i try to get the ['uniURL'] outside the foreach for a university it's not working. it looks like the array within $universiteiten isn't updated with a new associative value (['uniURL']) Like:

 

foreach( $universiteiten as $universiteitkey => $universiteit ) {

echo '<strong>' . $universiteit['uniNaam'] . ':' . '</strong> <br />';	
echo $universiteit;


    foreach( $hDirectory->hEntity as $school ) { 

	if ( $universiteit['uniID'] == $school->orgUnitId ) {

		$hURL = (string) $school->DirectoryURL;

		$universiteiten[$universiteitkey]['uniURL'] = $hURL; 
		echo  'Universiteit ID: ' . $universiteit['uniID'] . '<br />';
		echo 'Hodex URL: ' .  $hURL . '<br /><br />'; // this works fine
	}
        }
}

echo $hURL // doesn't work
echo $uniUtrecht['uniURL']; //here the output is missing 

 

Will output an error that the 'uniURL' is missing.. It seams like i cannot reach for the 'uniURL' outside of the IF Statement.

 

foreach() {

    foreach() {

          if() {

            here i can access the ['uniURL'] 

          }

        here i've lost ['uniURL'] 

    }

here i've lost ['uniURL'] 

}

here i've lost ['uniURL'] 

 

What am i doing wrong here?

 

 

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324247
Share on other sites

hmz..

 

Here's all of it:

 


// Functies inladen
include_once('INCLUDES/functions.php');

// Hodex XML bestanden in array stoppen 
// Bevat hodex URL's van alle scholen en school afkortingen
$hodexDirectory = get_xml_feed('coolfeed.xml');

///////////start//////////////
///Variabelen declareren///
/////////////////////////////

/// De Universiteiten
$universiteiten = array( 

$uniAmsterdamVan = array('uniNaam' =>'Universiteit van Amsterdam', 'uniID' => 'uva'),
$uniAmsterdamVrij = array('uniNaam' =>'Vrije Universiteit Amsterdam', 'uniID' => 'vu'),
$uniDelft = array('uniNaam' =>'Technische Universiteit Delft', 'uniID' => 'tud'),
$uniEindhoven = array('uniNaam' =>'Techische Universiteit Eindhoven', 'uniID' => 'tue'),
$uniGroningen = array('uniNaam' =>'RijksUniversiteit Groningen', 'uniID' => 'rug'),
$uniLeiden = array('uniNaam' =>'Universiteit Leiden', 'uniID' => 'lei'),
$uniMaastricht = array('uniNaam' =>'Maastricht University', 'uniID' => 'um'),
$uniNijmegen = array('uniNaam' =>'Radboud Universiteit Nijmegen', 'uniID' => 'run'),
$uniRotterdam = array('uniNaam' =>'Erasmus Universiteit Rotterdam', 'uniID' => 'eur'),
$uniTilburg = array('uniNaam' =>'Tilburg University', 'uniID' => 'uvt'),
$uniTwente = array('uniNaam' =>'Universiteit Twente', 'uniID' => 'ut'),
$uniUtrecht = array('uniNaam' =>'Universiteit Utrecht', 'uniID' => 'uu'),
$uniWageningen = array('uniNaam' =>'Wageningen University', 'uniID' => 'wur')

);


/////////////end/////////////
///Variabelen declareren///
/////////////////////////////

/// Aan iedere universiteit de HodexURL toevoegen
foreach( $universiteiten as $universiteitkey => $universiteit ) {

echo '<strong>' . $universiteit['uniNaam'] . ':' . '</strong> <br />';	

foreach( $hodexDirectory->hodexEntity as $school ) { 

	if ( $universiteit['uniID'] == $school->orgUnitId ) {

		$hodexURL = (string) $school->hodexDirectoryURL;
		$universiteiten[$universiteitkey]['uniURL'] = $hodexURL; 
		echo  'Universiteit ID: ' . $universiteit['uniID'] . '<br />';
		echo 'Hodex URL: ' .  $hodexURL . '<br /><br />'; 
	}

}
}


Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324251
Share on other sites

As before, you're dealing with copies of values, not values themselves.

 

Do away with all those $uniX variables and use array keys.

$universiteiten = array( 

"uniAmsterdamVan" => array('uniNaam' =>'Universiteit van Amsterdam', 'uniID' => 'uva'),
"uniAmsterdamVrij" => array('uniNaam' =>'Vrije Universiteit Amsterdam', 'uniID' => 'vu'),

echo $universiteiten["uniUtrecht"]["uniURL"];

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324260
Share on other sites

i'm sorry but i don't understand exactly.

What i think you are saying: Stop working with associative arrays within an array and change this:

 

$universiteiten = array( 

"uniAmsterdamVan" => array('uniNaam' =>'Universiteit van Amsterdam', 'uniID' => 'uva'),
"uniAmsterdamVrij" => array('uniNaam' =>'Vrije Universiteit Amsterdam', 'uniID' => 'vu'),

 

into multiple instances of $universiteit, like this: 

 

$universiteiten["uniAmsterdamVan"]["uva"]["http://#"];
$universiteiten["uniAmsterdamVrij"]["vu"]["http://#"];

 

i'm trying to understand here :)

 

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324290
Share on other sites

Kinda. Mostly I'm saying to start using associative arrays. Your earlier version

$universiteiten = array( 

$uniAmsterdamVan = array('uniNaam' =>'Universiteit van Amsterdam', 'uniID' => 'uva'),
$uniAmsterdamVrij = array('uniNaam' =>'Vrije Universiteit Amsterdam', 'uniID' => 'vu'),

is not (well, half not) associative. Those extra $uniX variables are just complicating things.

 

You should be accessing everything like

$universiteiten[name]["uniNaam" or "uniID" or "hURL"]

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324309
Share on other sites

He Requinix!

 

After some puzzeling it is finally working! Have run over your explanation a couple of times. Did some more googling/thinking and shabam!

 

a magic word missing here was the multidimensional array. Seems like what i'm using here is a multidimensional array with associative arrays within.

 

Thank you v/m, i will be back! :)

 

ps. how do you get those awesome colors inside your code here? using [ code -] won't do that :P

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324968
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.