Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working with CodeIgniter and have created a custom form preferences custom config. In that I have an array that is as follows:

Array
(
  [1] => Category 1
  [2] => Category 2
  [3] => Category 3
  [4] => Category 4
  [5] => Category 5   
)

I am passing that to the view as the var $service_categories what I'd then like to do is match it to the "value" that is in the database. I.E 5. If it matches then show Category 5 in the view. At the moment I am just showing 5 - This is no good to the user.

The variable $service->service_category is a number.

The var service produces:

Array
(
    [0] => stdClass Object
    (
        [service_id] => 3
        [organisation_id] => 2
        [service_name] => Edited Service 3
        [service_description] => This is service 3 provided by
        [service_category] => 5
        [service_metadata] => Metadata for service 3 - Edited
        [service_cost] => 22.00
        [service_active] => active
    )
)

My current PHP for this is as follows:

if (in_array($service->service_category, $service_categories))
{
   echo "Exists";
}

However, the Exists is not showing in the view. It is showing nothing at all.

Am I doing something wrong with the in_array method?

share|improve this question
can you post var_dump($service) here ? – Marek Sebera Jul 12 '12 at 11:06
Sure I've added it to the question – StuBlackett Jul 12 '12 at 11:09
On a side note: this is a very good way to ask a question, the information is thorough enough :) – Berry Langerak Jul 12 '12 at 11:10
the variable is a number but your comparing it with text 'Category 5' – Waygood Jul 12 '12 at 11:11
is the index the ID or just the index of the array (as there is no 0)? – Waygood Jul 12 '12 at 11:14
show 1 more comment

4 Answers

up vote 2 down vote accepted

in_array() checks if a value exists in the array. So in_array('Category 1', $service_categories) would work.

However, to check if a key is present in an array, you can use:

if(array_key_exists($service->service_category, $service_categories)) {
    echo "Exists";
}

I think, this is what you're looking for.

share|improve this answer
Brilliant, Thats bringing it back. I'll try working out how to match the names up now – StuBlackett Jul 12 '12 at 11:12
isset() could also be used. However, as isset() checks if the key exists and the value is not null, I would prefer using array_key_exists(), as it only checks for key existence. – Dennis Møllegaard Pedersen Jul 12 '12 at 11:26

The variable : $service->service_category is a number.

And that precisely is the problem: your testing to see if "5" is equal to "Category 5", which it is obviously not. Simplest solution would be to prepend the "5" with "Category ":

<?php
$category = 'Category ' . $service->service_category;

if (in_array($category, $service_categories)) {
   echo "Exists";
}

EDIT: If you want to check if the array key exists (because '5' => 'Category 5'), this could be achieved with isset( ) or array_key_exists.

<?php
if (array_key_exists ($service->service_category, $service_categories )) {
   echo "Exists";
}

// does the same:
if (isset ($service_categories[service->service_category] )) {
   echo "Exists";
}
share|improve this answer

I think array_key_exists is the function you might search.

share|improve this answer
if (isset($service_categories[$service->service_category])) {
   echo "Exists";
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.