new to PHP cicle and array problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Albertogomes
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Oct 30, 2012 8:28 am

new to PHP cicle and array problem

Postby Albertogomes » Tue Nov 06, 2012 8:23 am

for($j=0;$j<$i;$j++){
if($produto == $produtos[$j]){
echo $produto;
break;
}
else echo "error"; // errorerrorerroerror$produto if the last exists and error when the others are differnt
}
im trying to get a variable if exists in an array and get an error message if that variable doesn´t exists in the array. how can i do that? Thanks

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: new to PHP cicle and array problem

Postby simplypixie » Tue Nov 06, 2012 11:46 pm

Where is are your variables $i and $producto being defined (could do with seing them to make sure your loop will work in the first instance)?

Also, you don't break within your for loop so remove that.

MeroD
New php-forum User
New php-forum User
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: new to PHP cicle and array problem

Postby MeroD » Wed Nov 07, 2012 2:17 am

<?php
foreach ($produtos as $plist) {
  if ( in_array($plist, $produto) ) {
    echo $plist . " Found!<br />";
  }
  else {
    echo "Product not found!<br />";
  }
}
?>

http://www.w3schools.com/php/func_array_in_array.asp
http://www.php.net/manual/en/function.in-array.php


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests

cron