John Davidson

How can I search an array using an array list in php?

0 comments
Message:


Good day, guys!


Please, am working on a payment system that should be able to check and display the months of the year a member didn't make payment.


To achieve this, I will fetch the months of the year s/he made payment and run it through an array of months from January - December.


i.e.


$array_of_months = array('January','February','March','April','May','June','July','August','September','October','November','December');

$months[] = '';
$this_year = date('Y');
$stmt = $this->db->get_where('payment',['year'=>$this_year,'member_id'=>2]);
$result = $stmt->result();
foreach($result as $key => $val):
$months[] = $val->month;
endforeach;

To get the months that didn't match the months of the year paid for, I am using array_search() and the code is as shown below:


if($key = array_search($months, $array_of_months) !== false):
unset($arrays_of_months[$key]);
else:
echo 'Nil';
endif;

Instead of getting the result of the IF block, what am getting the result of the ELSE block.


This is the full code:


$arrays_of_months = array('January','February','March','April','May','June','July','August','September','October','November','December');
$months = array();
$this_year = date('Y');

$stmt = $this->db->get_where('payment',['year'=>$this_year]);
$result = $stmt->result();
foreach($result as $key => $val):
$months[] = $val->month;
endforeach;

if($key = array_search($months, $arrays_of_months) !== false):
unset($arrays_of_months[$key]);
else:
echo 'Nil';
endif;

Please, what am I not doing right?


Thank you..



My Answer:
conn->prepare("SELECT payment_month FROM payments WHERE member_id = :member_id AND payment_year = :payment_year");_$stmt->bindParam(':member_id', $member_id);_$stmt->bindParam(':payment_year', $this_year);_$stmt->execute();_$result = $stmt->fetchAll(PDO::FETCH_ASSOC);_$paid_months = array();_foreach($result as $row) {_$paid_months[] = $row['payment_month'];_}_$unpaid_months = array_diff($array_of_months, $paid_months);_foreach($unpaid_months as $month) {_echo $month . " ";_}_This code snippet fetches the months the member made payment for the current year and then compares it with the array of months from January to December to find the months the member did not make payment. Finally, it displays the unpaid months.

Rate this post

4 of 5 based on 8394 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us