[Solved] Output specified value from function with array – PHP [closed]

Introduction

This question is about how to output a specified value from a function with an array in PHP. This is a common problem that many developers face when working with arrays in PHP. In this article, we will discuss how to solve this problem and provide a few examples of how to do so. We will also discuss some of the best practices for working with arrays in PHP. By the end of this article, you should have a better understanding of how to output a specified value from a function with an array in PHP.

Solution


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

3

solved Output specified value from function with array – PHP [closed]


If you are looking for a way to output a specified value from a function with an array in PHP, then you have come to the right place. In this article, we will discuss how to do this in a few simple steps.

The first step is to create a function that takes an array as an argument. This function should loop through the array and return the value that you are looking for. Here is an example of such a function:

function getValueFromArray($array) {
    foreach($array as $value) {
        if($value == 'desired_value') {
            return $value;
        }
    }
}

In this example, the function will loop through the array and return the value that matches the desired value. Once you have created the function, you can call it with the array as an argument. Here is an example of how to do this:

$array = array('value1', 'value2', 'desired_value');
$value = getValueFromArray($array);
echo $value; // Outputs 'desired_value'

And that’s it! You have successfully outputted the desired value from a function with an array in PHP. If you have any questions or comments, please feel free to leave them in the comments section below.