Ben Thompson Ben Thompson - 7 months ago 8
PHP Question

Output specified value from function with array - PHP

I'm trying to write a function and get results through echo/return, by specified first value of array as below.

this is my code:

<?php
function getInfo($info) {
$info = array(
'some value',
'some value 2' => 'some sub-value'
);
return $info;
}
?>


and display it in site:

<div class="input">
<span><?php getInfo('some value'); ?></span>
</div>


but this does not work...
what can i do ?
thanks.

btw if can someone refer me to some really-good and advanced PHP guidance it will be really appreciated thanks.

Answer

Based on "if for example will be "=>" after the first one, or the second, that's what i want" i think you want to find out value from array based on the string passed to function, So do like below:-

<?php
function getInfo($string) {
   $info = array(
    'some value',
    'some value 2' => 'some sub-value'
   );
   return $info[$string];
}
?>

<div class="input">
    <span><?php echo getInfo('some value'); ?></span><!-- echo is missed-->
</div>

Note:- Make sure your each array index have some value otherwise nothing will print (in case here for some value).

Comments