Page 1 of 1

array | return $key

Posted: Tue Dec 07, 2004 1:45 pm
by ol4pr0
Oke i have this huge list of differant kinds of things.
the form itself is with javascript

so the $_POST['area'] is being returned in numbers 1 - untill Oeffff

So i am trying to compare the number with the area in this case and than have it return the area belonging to that number. however it keeps on returning Administración - Secretariado.

Am i missing some ?

Code: Select all

function selected_area($area) 
{
	$areas = array (
	'1' => 'Administración - Secretariado',
	'5' => 'Ciencias',
	'10' => 'Ciencias Educacionales',
	'15' => 'Ciencias Sociales - Humanidades - Filologías',
	'20' => 'Construcción - Arquitectura',
	'25' => 'Derecho',
	'30' => 'Dirección',
	'35' => 'Económicas - Empresariales',
	'40' => 'Ingenierías',
	'45' => 'Marketing - Comunicación',
	'50' => 'Oficios Profesionales',
	'55' => 'Producción - Calidad',
	'60' => 'Profesiones Liberales - Creativas',
	'65' => 'Recursos Humanos',
	'70' => 'Sanidad - Salud - Investigación',
	'75' => 'Tecnología - Informática - Internet',
	'80' => 'Transporte - Logística - Almacén',
	'85' => 'Turismo - Restauración',
	'90' => 'Ventas - Comercial');
	ksort ($areas);
	foreach ($areas as $keys => $area)
	{
		return $area;
	}
}
echo selected_area($_POST['area']);

Posted: Tue Dec 07, 2004 2:02 pm
by rehfeld
you can only return once inside a function. when you return, the function stops immediately.

since you never actually did any checking if the area was == to the area you wanted, it was just returning the first area in the loop.

try this

Code: Select all

function selected_area($area) 
{
    $areas = array (
    '1' => 'Administración - Secretariado',
    '5' => 'Ciencias',
    '10' => 'Ciencias Educacionales',
    '15' => 'Ciencias Sociales - Humanidades - Filologías',
    '20' => 'Construcción - Arquitectura',
    '25' => 'Derecho',
    '30' => 'Dirección',
    '35' => 'Económicas - Empresariales',
    '40' => 'Ingenierías',
    '45' => 'Marketing - Comunicación',
    '50' => 'Oficios Profesionales',
    '55' => 'Producción - Calidad',
    '60' => 'Profesiones Liberales - Creativas',
    '65' => 'Recursos Humanos',
    '70' => 'Sanidad - Salud - Investigación',
    '75' => 'Tecnología - Informática - Internet',
    '80' => 'Transporte - Logística - Almacén',
    '85' => 'Turismo - Restauración',
    '90' => 'Ventas - Comercial');
    ksort ($areas);

    if (isSet($areas[$area])) {
        return $areas[$area];
    } else {
         return false;
    }
}

// this would have worked too


foreach ($areas as $key => $value) {
    if ($area == $key) {
        return $value;
    }
}

Posted: Tue Dec 07, 2004 2:08 pm
by ol4pr0
I see what u mean .

Thanks alot.