PHP - Yuda

 
Vista:

Yuda

Publicado por victor (1 intervención) el 13/08/2007 23:52:03
look... this is my code:

<?
function searchstoreconcpt($yyear,$awcostt){
$j;
for($j=0; $j<=sizeof($awcostt);$j++)
{
if($awcostt[$j]["yyear"] == $yyear)
break;
}
if($j > sizeof($awcostt))
{
$awcost[$j]["yyear"] = $yyear;
return $awcost[$j];
}
else
{
$awcostt[$j]["yyear"] .= " + 1";
return $awcostt[$j];
}
}
?>

<pre>
<?
$i=1;
$awcost;
$awcost[$i++] = searchstoreconcpt(2003,$awcost);
$awcost[$i++] = searchstoreconcpt(2003,$awcost);
$awcost[$i++] = searchstoreconcpt(2002,$awcost);
$awcost[$i++] = searchstoreconcpt(2003,$awcost);
$awcost[$i++] = searchstoreconcpt(2003,$awcost);
$awcost[$i++] = searchstoreconcpt(2001,$awcost);

print_r($awcost);
?>
</pre>

and this is my result:

Array
(
[1] => Array
(
[yyear] => 2003
)

[2] => Array
(
[yyear] => 2003 + 1
)

[3] => Array
(
[yyear] => 2002
)

[4] => Array
(
[yyear] => 2003 + 1
)

[5] => Array
(
[yyear] => 2003 + 1
)

[6] => Array
(
[yyear] => 2001
)

[7] => Array
(
[yyear] => 2002 + 1
)

)
but...

I want this result....



Array
(
[1] => Array
(
[yyear] => 2003 +1 +1 +1
)

[2] => Array
(
[yyear] => 2002 +1 +1
)

[3] => Array
(
[yyear] => 2001
)

)

I don't know if you can help me, please, i'm waitting for your answer
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Yuda

Publicado por Diego Romero (1450 intervenciones) el 14/08/2007 03:17:50
It's pretty obious, my friend, every time you make an assigment to an array you are creating a new item in it, that's why you end up with 7 items and not 3.

Unless i'm wrong, what you want to do is find a value inside the array, and if it exists, add a string ("+ 1"), if it does not exist then assign a year. But you must rewrite that function.

function SearchItem($item, $work) {
$res = NULL;
foreach ($work as $key => $value) {
if ($value == $item) { $res = $key; break; }
}
return $res;
}
This function returns NULL if an item does not exist in array, otherwise returns its position.

then...

$exists = SearchItem("2003",$awcost);
if ($exists != NULL) { $awcost[$exists] .= " + 1"; } // note the dot
else { $awcost[] = "2003"; }

you can put this piece of code inside a bucle. Well, you get the picture now ;).
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar