Jump to content

How Do You Make Keys From An Array() Global?


monaya

Recommended Posts

I'm trying to access the variables in the array below outside of the function. I've tried everything :sweat:

 

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!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by Jessica
Link to comment
Share on other sites

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 by monaya
Link to comment
Share on other sites

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 by Jessica
Link to comment
Share on other sites

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 by monaya
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by monaya
Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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.

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.