Văn Lộc Văn Lộc - 1 month ago 8
PHP Question

How to set values of all element in array with loop conditional?

I have an array

listProduct
. I want to loop through all element in this array and set condition
itemID
in
listProduct
is 0 will print a text.

Like this:

foreach (listProduct as $item)
if($item->proCode == 0)
echo "true";
elseif ($item->proCode == 1)
echo "false";
else
echo "nothing";
endif


Problem is:

It will loop through this array, and this only compares the last item of an array with conditional.

If I add a
break
after
echo
command, it will fail at first element of an array.

I had a GUI when I clicked a button
Edit
. I want to check at this
proCode
is 0. It only loads any element, if
proCode
is 1, it loads another element.

Have any method to loop through sequence element in an array.

Updated 1:

I think my question is unclear.

So, my case needs resolve with
id
. This mean, when to click an
Edit
button, get
id
to check in conditional.

Don't need any loop in this case.

Thanks, #FrayneKonok, #Barmar and #Abolarinstephen.

Answer

Try this:

The array must have an index 'proCode'

I am assuming this is a 2D array

$id = $_GET['id'];

if ($listProduct[$i]['proCode'] == 0):
    echo "True";
elseif ($listProduct[$i]['proCode'] == 1):
    echo "False";
 else:
     echo "Nothing";
 endif;