monaya Posted December 29, 2012 Share Posted December 29, 2012 I'm trying to access the variables in the array below outside of the function. I've tried everything The variables I'm trying to access are bold and in red in the code below. These variables are just not accessible outside the function. function GenerateSitemap($params = array()) { // default parameters extract(shortcode_atts(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params)); // create sitemap $sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0"); if ($sitemap != '') { $sitemap = ($title == '' ? '' : "<h2>$title</h2>") . '<ul' . ($id == '' ? '' : " id=\"$id\"") . ">$sitemap</ul>"; } return $sitemap; } add_shortcode('sitemap', 'GenerateSitemap'); Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/ Share on other sites More sharing options...
trq Posted December 29, 2012 Share Posted December 29, 2012 You need to return these variables (within an array) from the function. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402089 Share on other sites More sharing options...
monaya Posted December 30, 2012 Author Share Posted December 30, 2012 They're already in an array, no? Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402125 Share on other sites More sharing options...
timothyarden Posted December 30, 2012 Share Posted December 30, 2012 Not entirely sure of what your trying to achieve as you have some functions in there which you have written yourself but here is some advice (under the code). <?php function GenerateSitemap($params = array()) { //1 // default parameters $array = extract(shortcode_atts(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params)); // 2 //create sitemap $sitemap = wp_list_pages("title_li=&depth=\$depth&sort_column=menu_order&echo=0"); // 3 if ($sitemap != '') { $sitemap = ($title == '' ? '' : "<h2>".$title."</h2>") . '<ul' . ($id == '' ? '' : " id=\"".$id."\"") . ">".$sitemap."</ul>"; } //4 return $array, $sitemap; //Or global $array; } // 5 add_shortcode('sitemap', 'GenerateSitemap'); ?> 1. Assign the value returned by extract to a variable 2. Not Sure what this function does as it is not defined in the code given 3. If sitemap is not equal to nothing - meaning it is equal to something - then change its value. Are you sure its not if it equals nothing then change it? 4. Either return too values (which returns them in array - and yes you can have an array inside an array) or just make array global as is done below If you use the first method to return the 2 values just reference to the as $result = GenerateSitemap($parameters); and then $result[0] (1st value) and $result[1] (second value) To reference to the array inside $result[0] use a second box brackets - Example: $result[0]['title'] 5. Not sure what this function does as it is not defined in the code given Hope this helps Timothy Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402131 Share on other sites More sharing options...
timothyarden Posted December 30, 2012 Share Posted December 30, 2012 Wait, do you mean individual keys from the array global or the entire array? Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402133 Share on other sites More sharing options...
monaya Posted December 31, 2012 Author Share Posted December 31, 2012 I mean individual keys from the array Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402447 Share on other sites More sharing options...
Jessica Posted December 31, 2012 Share Posted December 31, 2012 (edited) function GenerateSitemap($params = array()) { // default parameters extract(shortcode_atts(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params)); // create sitemap $sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0"); if ($sitemap != '') { $sitemap = ($title == '' ? '' : "<h2>$title</h2>") . '<ul' . ($id == '' ? '' : " id=\"$id\"") . ">$sitemap</ul>"; } return $sitemap; } add_shortcode('sitemap', 'GenerateSitemap'); Right now, your code returns a variable it never does anything with. If you want to use something you create within a function outside of that function, return it and assign it to a variable. However for what you're doing it makes more sense to define your default parameters as constants outside of the function. Edited December 31, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402450 Share on other sites More sharing options...
monaya Posted December 31, 2012 Author Share Posted December 31, 2012 (edited) Can't seem to make it work. The main code is this: function GenerateSitemap($params = array()) { //1 // default parameters $array = extract(shortcode_atts(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params)); global $array; } I'd like to use the title, id or depth KEYS inside a second function. They just come up empty. Edited December 31, 2012 by monaya Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402451 Share on other sites More sharing options...
Jessica Posted December 31, 2012 Share Posted December 31, 2012 (edited) Your array keys are strings. You can just type 'title'. That is the key. The value of the array at that key is ALSO just a string, you could type. You're asking the question wrong, so no one understands what you want. Did you even read my post? Because the only thing I can think you're trying to do, you should do with constants. I don't understand why no one else has asked you yet - why are you using extract on an array you just created, and never using the array??? Finally, (hopefully my last edit), we don't know what shortcode_atts does. Edited December 31, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402452 Share on other sites More sharing options...
monaya Posted December 31, 2012 Author Share Posted December 31, 2012 (edited) I am using the array later on when I access that function. I can print the key by simply using $title, but I cannot do that in a second function. It simply comes up blank. For example, I tried stripping away all the unnecessary code to demonstrate the problem <?php function GenerateSitemap($params = array()) { $array = extract(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params); print $title; } function secondfunction() { print $title; } GenerateSitemap() // WORKS it prints the $title key secondfunction() //Throws an error $title key is not accessible ?> In the second function $title is empty. P.S. shortcode_atts is a Wordpress function http://codex.wordpre.../shortcode_atts Edited December 31, 2012 by monaya Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402461 Share on other sites More sharing options...
trq Posted December 31, 2012 Share Posted December 31, 2012 In the second function $title is empty. Indeed. Read this, functions. You don't seem to have an understanding of functions. Functions have there own scope and encapsulate the functionality the provide. To do what you want, GenerateSitemap() needs to return the data you need, while secondfunction() needs to accept an argument so you can pass this data in to it. The end result would look something like: <?php $title = GenerateSitemap(); secondfunction($title); Having said that however, it seems GenerateSitemap() is a pretty silly name for a function that returns some $title data. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402463 Share on other sites More sharing options...
monaya Posted December 31, 2012 Author Share Posted December 31, 2012 If you run the code you'll see that GenerateSitemap(); does return data and the secondfunction still comes up empty even if you pass the the $title argument through the function. [/font][/color] Indeed. Read this, functions. You don't seem to have an understanding of functions. Functions have there own scope and encapsulate the functionality the provide. To do what you want, GenerateSitemap() needs to return the data you need, while secondfunction() needs to accept an argument so you can pass this data in to it. The end result would look something like: <?php $title = GenerateSitemap(); secondfunction($title); Having said that however, it seems GenerateSitemap() is a pretty silly name for a function that returns some $title data. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402465 Share on other sites More sharing options...
Jessica Posted December 31, 2012 Share Posted December 31, 2012 If you refuse to learn, people will stop trying to help you eventually. I would have said quickly, but there are plenty of people here who tolerate intentional helplessness for longer than I do. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402470 Share on other sites More sharing options...
monaya Posted December 31, 2012 Author Share Posted December 31, 2012 (edited) Ok Thank you. I must be misunderstanding something. Sorry for the confusion. For future reference for anyone else who stumbles on the post, here's the final code, still not working. It's just sample code to see if I could access the keys of an array from one function to another. <?php function firstfunction() { global $array; $array = extract(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ), $params); print $title; } function secondfunction($title) { print $title; } firstfunction(); //works. prints the key secondfunction($title); //empty ?> Edited December 31, 2012 by monaya Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402471 Share on other sites More sharing options...
trq Posted December 31, 2012 Share Posted December 31, 2012 Your GenerateSitemap() function doesn't return anything. It outputs a string. Even if it did return data, your not capturing it. ps: Forget the global keyword exists. It breaks the encapsulation that functions are intended to provide. A working example: [color=#666600]<?[/color][color=#000000]php[/color] [color=#000088]function[/color][color=#000000] firstfunction[/color][color=#666600]()[/color] [color=#666600]{[/color] [color=#000000] $array [/color][color=#666600]=[/color][color=#000000] extract[/color][color=#666600]([/color][color=#000000]array[/color][color=#666600]([/color] [color=#008800] 'title'[/color][color=#000000] [/color][color=#666600]=>[/color][color=#000000] [/color][color=#008800]'Site map'[/color][color=#666600],[/color] [color=#008800] 'id'[/color][color=#000000] [/color][color=#666600]=>[/color][color=#000000] [/color][color=#008800]'sitemap'[/color][color=#666600],[/color] [color=#008800] 'depth'[/color][color=#000000] [/color][color=#666600]=>[/color][color=#000000] [/color][color=#006666]2[/color][color=#000000] [/color][color=#666600])[/color] [color=#666600] );[/color] [color=#000088] return[/color][color=#000000] $title[/color][color=#666600];[/color] [color=#666600]}[/color] [color=#000088]function[/color][color=#000000] secondfunction[/color][color=#666600]([/color][color=#000000]$title[/color][color=#666600])[/color] [color=#666600]{[/color] [color=#000088] print[/color][color=#000000] $title[/color][color=#666600];[/color] [color=#666600]}[/color] [color=#000000]$title = firstfunction[/color][color=#666600]();[/color] [color=#000000]secondfunction[/color][color=#666600]([/color][color=#000000]$title[/color][color=#666600]);[/color][color=#000000] [/color][color=#880000]//empty[/color] [color=#666600]?>[/color] [color=#666600] [/color] Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402479 Share on other sites More sharing options...
trq Posted December 31, 2012 Share Posted December 31, 2012 I give up on this editor. <?php function firstfunction() { $array = extract(array( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ) ); return $title; } function secondfunction($title) { print $title; } $title = firstfunction(); secondfunction($title); ?> Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402480 Share on other sites More sharing options...
Jessica Posted December 31, 2012 Share Posted December 31, 2012 You also keep saying "access the keys of an array." That means NOTHING. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402482 Share on other sites More sharing options...
Christian F. Posted December 31, 2012 Share Posted December 31, 2012 trq: That's why I always keep the Rich-text editor turned off at all times, makes my life a lot easier when posting. monaya: As Jessica asked you previously, why are you using extract () in your function? It proved you with no benefits, and it makes no sense at all. Just do something like this instead: function generate () { $myData = array ( 'title' => 'Site map', 'id' => 'sitemap', 'depth' => 2 ); return $myData['title']; } As previously stated: Read the PHP manual, learn how to use it to learn about the stuff you're not familiar with. Use the examples there and play around with the code. Observe what happens when you change something, and only change one thing at a time. Most importantly though: Take the time to actually plan how the code will/should/needs to work, before sitting down and actually writing it. The more time you take to plan every detail, the less frustrations and problems you will have when it comes to writing the code itself. Quote Link to comment https://forums.phpfreaks.com/topic/272500-how-do-you-make-keys-from-an-array-global/#findComment-1402484 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.