0

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
        )

)
2
  • And how is PHP supposed to know which index is to be printed? Commented May 16, 2012 at 15:16
  • So you're wanting to retrieve the value of Question by providing the value of Field? Commented May 16, 2012 at 15:22

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.