0

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?

6
  • can you post var_dump($service) here ? Commented Jul 12, 2012 at 11:06
  • Sure I've added it to the question Commented Jul 12, 2012 at 11:09
  • On a side note: this is a very good way to ask a question, the information is thorough enough :) Commented Jul 12, 2012 at 11:10
  • the variable is a number but your comparing it with text 'Category 5' Commented Jul 12, 2012 at 11:11
  • is the index the ID or just the index of the array (as there is no 0)? Commented Jul 12, 2012 at 11:14

4 Answers 4

4

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.

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

2 Comments

Brilliant, Thats bringing it back. I'll try working out how to match the names up now
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.
4

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";
}

Comments

2

I think array_key_exists is the function you might search.

Comments

2
if (isset($service_categories[$service->service_category])) {
   echo "Exists";
}

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.