0

I have a database table with options that looks like this

Database records

I do a SELECT * FROM options and load all records into a recordset.

How do I then access just the optionValue of optionName = SMSactive

[EDIT]

Yes, I could do SELECT * FROM options WHERE optionName ='SMSactive' - I know that.

But as I said, since I have a recordset with a bunch of rows in it (from the SELECT), I want to pull a specific row where optionName = SMSactive

[EDIT] If you are using PHP >= 5.5 you can use array_column() http://php.net/array_column

But if not, the question has already been asked and answered here PHP multidimensional array search by value

Community
  • 1
  • 1
Steve
  • 1,371
  • 1
  • 16
  • 38

1 Answers1

2

To access to all records with optionName = SMSactive the SQL should be:

SELECT * FROM `TableName` WHERE `optionName` = 'SMSactive'

if your array result from the query is something like this:

$array = array(
    0 => array(
        'optionName' => 'lala0',
        'optionDescription' => 'lala01',
        'optionValue' => 'lala03',
    ),
    1 => array(
        'optionName' => 'lala1',
        'optionDescription' => 'ala2',
        'optionValue' => 'SMSactive',
    )
);

You can grab who has 'optionValue' => 'SMSactive', by this way

foreach ($array as $key) {
    if ($key['optionValue'] == 'SMSactive') {
        $filtered[]=$key;
    }
}

echo '<pre>';
print_r($filtered);
echo '</pre>';

Side Note: If the array is very large you have to be careful with the memory...it's advisable just get from the db what you are using in the moment...IMO

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • Modified from the movie Dodgeball - If you can do a write a SELECT statement, you can write a WHERE clause. – user2910265 Dec 16 '13 at 02:31
  • maybe I didn't make myself clear. I already do SELECT * FROM options. I have all rows in the array. Now I just want to grab one of them without doing another call to the database. – Steve Dec 16 '13 at 02:34
  • @EmilioGort it's a movie reference. You have to be familiar with the movie to understand what I wrote. – user2910265 Dec 16 '13 at 02:36
  • If that is how you want to phrase it, yes, I need to find that option in the multi dimensional array. – Steve Dec 16 '13 at 02:38
  • Thanks Emilio and it is correct but I knew how to do that. I was hoping there might be a way to search the array directly. I am using PHP < 5.5 or I could use array_column. In fact, this is a more elegant way of doing it using your technique http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search – Steve Dec 16 '13 at 03:12
  • the another way I see is using array filter with callback function but at the end is the same – Emilio Gort Dec 16 '13 at 03:15