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?

Udders
  • 259
  • 5
  • 16
  • 1
    Check the code/documentation, you are likely in need of array like `array('sector' => array('IT', 'MEDIA', 'ADVERTISING'))` – mkilmanas Jun 07 '11 at 15:18

4 Answers4

3

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

Crashspeeder
  • 4,291
  • 2
  • 16
  • 21
  • 1
    gee.. And I was like 5sec from submitting a "solution" :) – mkilmanas Jun 07 '11 at 15:16
  • +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 – KingCrunch Jun 07 '11 at 15:17
  • @Jordan - it answers the question by saying that it isn't possible, which is a 100% correct answer – Mark Baker Jun 07 '11 at 15:18
  • how can I do multiple likes using codeigniter active recorder based on the exploded string, which can be variable in length.....so stuck! – Udders Jun 07 '11 at 15:20
  • 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. – Jordan Running Jun 07 '11 at 15:21
  • The problem is that Udders didn't ask what he meant to ask. The question he wants to ask is "How do I do ____ with codeigniter" and provide the above sample code. If it said codeigniter I wouldn't have answered. The user's question as stated has no answer but to change the like() function, or he misunderstands how the like() function works on the db object. – Crashspeeder Jun 07 '11 at 15:26
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')
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Dragos
  • 138
  • 4
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"
  }
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
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%';
Kien Nguyen
  • 398
  • 4
  • 12