Hi-

I am having difficulty working with an array in php and would appreciate any help...thanks in advance!

I have queried my database to return all product ids with type 'book' and id base 'brd'. The product id is a string that is formatted as follows: <base><3 or 4 digits><no letter or A or B or C...> (e.g., brd805, brd805A, brd805B, brd1002, brd105B).

I would like to make a new version of the array where the numeric part is unique. In the example below, I would like to remove 'brd805A' and 'brd805B' from the array because 'brd805' exists. ...but I want to keep 'brd142B' in the array because there is no 'brd142'...

Is my only option to loop through the array in php, compare the numeric portion and then create a new array based on findings?

Is there a regular expression I could use in my sql query to output the corrected array rather than trying to work with it in php?

example output from my database query:
array (
0 =>
array (
'ID' => 'brd805',
'Type' => 'book',
),
1 =>
array (
'ID' => 'brd805A',
'Type' => 'book',
),
2 =>
array (
'ID' => 'brd805B',
'Type' => 'book',
),
3 =>
array (
'ID' => 'brd823',
'Type' => 'book',
),
4 =>
array (
'ID' => 'brd1023',
'Type' => 'book',
),
5 =>
array (
'ID' => 'brd142B',
'Type' => 'book',
),
)

I realize I can use regexp in sql and in php, just couldn't find an expression to use... Is there an expression that you have in mind?
thanks-

Member Avatar for diafol

There's a link here:

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

but you may have seen it.

I can't think of how you'd do that, but if it was me, I'd take xan's advice and use php although it may add a slight overhead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.