DocM3 Posted March 4, 2012 Share Posted March 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/ Share on other sites More sharing options...
requinix Posted March 5, 2012 Share Posted March 5, 2012 $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? Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1323939 Share on other sites More sharing options...
DocM3 Posted March 5, 2012 Author Share Posted March 5, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324247 Share on other sites More sharing options...
requinix Posted March 5, 2012 Share Posted March 5, 2012 As long as there is at least one school, $hURL should work. However it'll be the very last one found. But where's $uniUtrecht coming from? I don't see it defined anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324249 Share on other sites More sharing options...
DocM3 Posted March 5, 2012 Author Share Posted March 5, 2012 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 />'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324251 Share on other sites More sharing options...
requinix Posted March 5, 2012 Share Posted March 5, 2012 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324260 Share on other sites More sharing options...
DocM3 Posted March 5, 2012 Author Share Posted March 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324290 Share on other sites More sharing options...
requinix Posted March 5, 2012 Share Posted March 5, 2012 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"] Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324309 Share on other sites More sharing options...
DocM3 Posted March 7, 2012 Author Share Posted March 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324968 Share on other sites More sharing options...
requinix Posted March 7, 2012 Share Posted March 7, 2012 Right, multidimensional. You were actually already doing that but I guess you didn't quite realize it. Anyway, use to get the syntax highlighting. Quote Link to comment https://forums.phpfreaks.com/topic/258277-foreach-scope/#findComment-1324973 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.