...

View Full Version : Returning values from functions syntax problem



matak
07-20-2007, 08:42 PM
Here's a simple explanation of what i don't understand. Probably beacouse i didn't read good enough when i learned php.

Code is basicly a function that returns array, which parts i need to use in another function. Like this



$array = array ("link1" => "value1", "link2" => "value2");

function getKeys ($value) {
return array_keys($value); #this is supposed to get an array like "link1", "link2"
}

//function which calls getKeys and is supposed to use that array

function useKeys($array) {
echo getKeys($array)[0];
}

I guess that's a syntax problem, but i just can't figure it out.
Thanks in advance!
(p.s. i don't have any rep to give you, but i would appreciate your help)

Mwnciau
07-20-2007, 09:18 PM
$array = array ("link1" => "value1", "link2" => "value2");

function useKeys($array) {
$arraykeys = array_keys($array);
echo $arraykeys[0];
}

You dont really need that first function.

matak
07-20-2007, 09:22 PM
$array = array ("link1" => "value1", "link2" => "value2");

function useKeys($array) {
$arraykeys = array_keys($array);
echo $arraykeys[0];
}

You dont really need that first function.

i need to figure that from post #1. i need it in another contex, this is just simple example..

Fumigator
07-20-2007, 09:42 PM
echo getKeys($array)[0];


Yeah you can't reference an array in this fashion; I know it looks like you should be able to do it since you can do echo $anArray[0]; and the result of calling getKeys() is an array, but that is just the point-- the result of getKeys() is an entire array object; you can't just append an index to an array object.

I'm not explaining it very well, I know... just think of $anArray[0] as its own independant variable that is encompassed by the array object $anArray.

I'm still not explaining it very well :D But this is a simple problem to circumvent. Simply assign the result of the function to a variable and then access the elements inside the array via the new variable.


$returnArray = getKeys($array);
echo $returnArray[0];

matak
07-20-2007, 10:00 PM
I think i took a much larger bite than i can swallow. I hope you understand that fraze. It's just that everything is so messy inside my head, i can't pull single straight thought, without loosing the big picture. I think that i maxed out my brain capacity and it's going to shutdown really soon.

And all what i'm trying to do is create and unordered list from X arrays. And there is so many problems, and that foreach just sux. It's not good for nothing. Sorry for venting, but just the thought of learning oop makes me sick.

sending kill signal.. :(

why isn't just a thought, or a wish to do something good enough to do the same. bleeep..

matak
07-20-2007, 11:07 PM
OK, after a hard days night.. Few minutes after the vent post i got an idea. It's really strange but i usually get new ideas right after posting my problem on forums. And ppl say that we are not only one :D j/k

I figured out a way to solve my situation, and that is calling a function froma an array. Sounds crazy, but it should work, and with your help i might even manage it. Here's the deal


<?php

$croatianlang = "čćđ";
$variable = callSub($croatianlang);

function callSub ($croatianlang){

echo "Yes";

}
$array2 = array ("sslink1" => $variable, "sslink2" => "ssvalue2");


function createMenu ($array2) {

foreach ($array2 as $key => $value) {

echo "<ul>\n";
echo "\t<li>".$key."\n";

echo "\t\t<ul>\n";
echo "\t\t\t<li>".$value."</li>\n";

echo "\t\t</ul>\n";
echo "\t</li>\n";
echo "</ul>\n";

}


}

?>

But the problem is that that "Yes" echos outside the foreach loop, and i need a way to set that $variable, before looping.

What am i doing wrong?

matak
07-20-2007, 11:27 PM
Solved :D :D :D

Return instead of echo :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum