0

I have a string that looks like this,

IT, MEDIA, ADVERTISING

I am then doing the following code.

$criteria = explode(",", $string);

This obviously creates the following when print_r is run over the $criteria.

array([0] => IT, [1] => MEDIA, [2] => 'ADVERTISING')

I using $criteria to match for keywords in a database in codeigniter application, I am wanting to using something like,

$this->db->like($criteria);

for this to work though I need the $criteria array to look like this,

array([sector] => IT, [sector] => MEDIA, [sector] => 'ADVERTISING')

How can I do this?

1
  • 1
    Check the code/documentation, you are likely in need of array like array('sector' => array('IT', 'MEDIA', 'ADVERTISING')) Commented Jun 7, 2011 at 15:18

4 Answers 4

3

PHP can't have multiple values with the same key.

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

6 Comments

gee.. And I was like 5sec from submitting a "solution" :)
+1 Nothing to include, except no language is able to do this. :) @Jordan: It does answer the question; its simply not possible to create such an array. Its like asking "How to fly to the moon with my car?". The only answer is "No way!" :D
@Jordan - it answers the question by saying that it isn't possible, which is a 100% correct answer
how can I do multiple likes using codeigniter active recorder based on the exploded string, which can be variable in length.....so stuck!
It does not solve the user's problem, which is how to perform this query in CodeIgniter using the given $string or $criteria. This "answer" is no better than "RTFM" at helping the poster more toward a solution to his actual problem.
|
1

You cannot build an array like this. The keys of the array are identical, so the values will overwrite each other.

Also I think that the method you are looking for is where_in():

$names = array('Frank', 'Todd', 'James');

$this->db->where_in('username', $names);

// Produces: WHERE username IN ('Frank', 'Todd', 'James')

Comments

0

If you're trying to make OR WHERE clauses with an array like this:

$criteria = array(
    0 => 'IT',
    1 => 'MEDIA',
    2 => 'ADVERTISING',
)

You can loop through it and use like() and or_like() methods:

foreach ($criteria as $index => $value) {
    if ($index == 0)
        $this->db->like('sector', $value);
    else
        $this->db->or_like('sector', $value);
}

// Produces WHERE sector LIKE '%IT%' OR sector LIKE '%MEDIA%' OR sector LIKE '%ADVERTISING%';

Comments

0

How about creating your array criteria like this:

$criteria = array('sector'=>explode(",", $string));

OUTPUT

var_dump($criteria);

array(1) {
  ["sector"]=>
  array(3) {
    [0]=>
    string(2) "IT"
    [1]=>
    string(6) " MEDIA"
    [2]=>
    string(12) " ADVERTISING"
  }
}

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.