Perminder Singh Klair
Perminder Singh Klair

Reputation: 11

Echo value of an array without Loop

I have an array like the example shown below. I want to echo a specific value. Right now what I am doing is:

$array[0]->Question

So, it outputs: Question1

But I dont want this solution.

Is there any way to echo specific value without key, for example (its not solution):

$array[]->Field == 'Field1' ? $array[]->Question : '';

Please suggest a solution.

Thanks!

[Required] => Array
(
    [0] => stdClass Object
        (
            [Field] => Field1
            [Question] => Question1
            [DataType] => Boolean
        )

    [1] => stdClass Object
        (
            [Field] => Field2
            [Question] => Question2
            [DataType] => varchar
        )

    [2] => stdClass Object
        (
            [Field] => Field3
            [Question] => Question3
            [DataType] => Boolean
        )

    [3] => stdClass Object
        (
            [Field] => Field4
            [Question] => Question5
            [DataType] => Int
        )

)

Upvotes: 0

Views: 2079

Answers (3)

Madbreaks
Madbreaks

Reputation: 19539

It sounds like you want to get the value of Question by providing Field. Try something like:

function getQuestion($field, $array){
    // Default to false if value not found
    $question = false;

    foreach($array as $data){
        if($data->Field == $field){
            $question = $data->Question;
            break;
        }
    }

    return $question;
}

...and then:

if($question = getQuestion('Feild1', $required)){
   // Do something with it...
}

There are a lot of ways to do this, but this simple approach should work.

Cheers

Upvotes: 1

Buddy
Buddy

Reputation: 1836

Check whole data set and return matching occurrences taking advantages of PHP 5.3.

$arr = array(
    0 => (object) array('Field' => 'Field1', 'Question' => 'Question1', 'DataType' => 'Boolean'),
    1 => (object) array('Field' => 'Field2', 'Question' => 'Question2', 'DataType' => 'varchar')
);

function get_value($needle, $haystack)
{
    return array_filter($haystack, function($item) use ($needle) {
        return in_array($needle, get_object_vars($item));
    });
}

print_r(get_value('Field1', $arr));

Output:

Array
(
    [0] => stdClass Object
        (
            [Field] => Field1
            [Question] => Question1
            [DataType] => Boolean
        )

)

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88647

This is basically the same operation as a simple SQL SELECT query. This function will provide a similar result:

function array_select ($array, $searchField, $searchVal, $selectField = '*') {

  // Loop the "table" (array)
  foreach ($array as $obj) {

    // If the "row" (item) is an object, has the field present and the value matches...
    if (is_object($obj) && isset($obj->{$searchField}) && $obj->{$searchField} == $searchVal) {

      // Return the value of the requested field, or the entire "row" if * was passed
      return $selectField == '*' ? $obj : (isset($obj->{$selectField}) ? $obj->{$selectField} : NULL);

    } // if

  } // foreach

  // We didn't find it
  return NULL;

}

Use it like:

if (($val = array_select($array, 'Field', 'Field1', 'Question')) !== NULL) {
  // Value was found
  echo $val;
} else {
  // Value not found
  echo "Not found!";
}

This is roughly the same as the following SQL query:

SELECT Question
FROM $array
WHERE Field = 'Field1'

This also supports passing '*' to or omitting the last parameter to return the entire object, which is roughly the same as:

SELECT *
FROM $array
WHERE Field = 'Field1'

See it working

Upvotes: 1

Related Questions