Page 1 of 1
Finding and extracting parts of a string
Posted: Tue Mar 25, 2003 4:10 am
by bluenote
Hi to all,
i posted this prob earlier but might to the wrong forum, or i have simply explained it not properly... here it is, again:
As a result of a query, there is the variable $projects
Code: Select all
<?php
$projects = "01,01,02,03,08,14,02";
?>
To process $projects any further, it
must 
look like this:
Code: Select all
<?php
$projects = "01,02,03,08,14";
?>
So if a number (like
01 or
02 in this example) occurs two (or more) times, the second (third, fourth and so on) occurence should be extracted and deleted. Both the sequence and the quantity of numbers in $projects are
unpredictable - it can start with
99, followed by
37 [...] and end up with
13, and there can be
66 as well as
3 or
0 numbers.

Has anyone an idea?
greez, bluenote
Posted: Tue Mar 25, 2003 5:47 am
by Ebula
use explode to turn the string into an array, next use array_unique to get rid of double values.
Posted: Tue Mar 25, 2003 7:01 am
by bluenote
Hi,
thanx for the fast answer. I have done the following:
Code: Select all
<?php
echo "<INPUT TYPE="HIDDEN" NAME="projects" VALUE="";
$projects_IDS = "02,04,03,02";
$projects_array = explode(",", $projects_IDS);
sort($projects_array);
$projects = array_unique($projects_array);
echo $projects;
echo "">\n";
?>
and the result is
Code: Select all
<INPUT TYPE="HIDDEN" NAME="projects" VALUE="ArrayArrayArrayArray">
instead of
Code: Select all
<INPUT TYPE="HIDDEN" NAME="projects" VALUE="02,03,04">
Where am I wrong?
Posted: Tue Mar 25, 2003 7:05 am
by twigletmac
You need to
implode() $projects_array to make it back into a string:
Code: Select all
$projects_array = array_unique($projects_array);
$projects = implode(',', $projects_array);
echo $projects;
Mac
Posted: Tue Mar 25, 2003 7:13 am
by bluenote
Hello,
after adding the implode() function...
Code: Select all
<INPUT TYPE="HIDDEN" NAME="projects" VALUE=",02,04,0302">
Posted: Tue Mar 25, 2003 7:20 am
by twigletmac
With this code:
Code: Select all
<?php
echo "<INPUT TYPE="HIDDEN" NAME="projects" VALUE="";
$projects_IDS = "02,04,03,02";
$projects_array = explode(",", $projects_IDS);
sort($projects_array);
$projects_array = array_unique($projects_array);
$projects = implode(',', $projects_array);
echo $projects;
echo "">\n";
echo $projects;
?>
I get the expected result - is there perhaps some other code which could be causing your result to be a bit strange?
Mac
Posted: Tue Mar 25, 2003 7:25 am
by Ebula
use print_r($project) to look at what your array looks like. You might have to pop the first entry du to a bug in the array_unique function.
Ebula
Posted: Tue Mar 25, 2003 7:32 am
by bluenote
Perhaps

it's the for {} Loop all the things happen in... here is the complete listing:
Code: Select all
<?php
$creator_num = $_REQUEST["creator_num"];
echo "<INPUT TYPE="HIDDEN" NAME="projects" VALUE="";
for ($i=0; $i < $creator_num; $i++) {
$qq = "idcreator_query".$i;
$qe = "idcreator_erg".$i;
$qn = "idcreator_numrows".$i;
$creatornfamily = $_REQUEST["creatornfamily_$i"];
$creatorngiven = $_REQUEST["creatorngiven_$i"];
$creatorini = $_REQUEST["creatorini_$i"];
${$qq} = "SELECT staff.s03, staff_departments.dep_s03 FROM staff LEFT JOIN staff_departments ON staff.s03 = staff_departments.dep_s03 WHERE (staff.s05 = '$creatornfamily' AND staff.s06 = '$creatorngiven' AND staff.s07 = '$creatorini') AND (staff.s03 != '') AND (staff_departments.dep_s03 != '')";
${$qe} = MYSQL_QUERY(${$qq});
${$qn} = MYSQL_NUM_ROWS(${$qe});
if (${$qn} > '0') {
$idcreator = "idcreator_".$i;
${$idcreator} = 0;
$p_IDS = "idcreator_dep_IDS".$i;
${$p_IDS} = mysql_result(${$qe},${$idcreator},"dep_s03");
$subpro = ${$p_IDS};
$loop_count = $creator_num - 1;
if ($i < $loop_count) {
$projects_IDS = "$subpro,";}
else if ($i==$loop_count) {
$projects_IDS = "$subpro";}
$projects_array = explode(",", $projects_IDS);
sort($projects_array);
$projects = array_unique($projects_array);
$projects = implode(',', $projects_array);
echo $projects;}
else {}}
echo "">\n";
?>
For explanation: One step before, an editor can insert names (last, first, initials) in a form. The number of names is variable, and for each name set, a query must be executed to retrieve the project numbers ($project) the person with the name $creatornfamily.$i creatorngiven.$i ... is working at.
Posted: Sat Mar 29, 2003 1:50 am
by bluenote
Hi and thanx to all who helped,
I have the

solution... it's nearly as simple as i was a blockhead. After renaming the hidden field to "pro_IDS" and processing it simply one form step further, i've done
Code: Select all
<?php
$pro_IDS = $_REQUEST["pro_IDS"];
$pro_IDS_array = explode(",", $pro_IDS);
sort($pro_IDS_array);
$pro_IDS_array = array_unique($pro_IDS_array);
$projects = implode(',', $pro_IDS_array);
?>
... adn this works fine. Thanx again to all. Vodka helps
bluenote