Search the Community
Showing results for tags 'php array'.
-
I'm trying to unset one record from array but I can't manage it, so I'm asking for someone to help me. Logic I'm trying to achieve is: User has an item with possible three categories, he posts a form and than I iterate over array and try to see if category3 matches, if not I go to category2, if not I go to category1. If I find one of the categories that matches array and category asked in form I proceed , but If I don't find any of the categories I unset that record in array.Below you will see an example what I've done now. I can't get the proper result. Either I unset all the records or unset the record that should stay. I'm trying different conditions for last three days without success. Record I'm talking about is with a key 5. This is my array: array (size=5) 0 => array (size=9) 'cost_no' => string 'D01' (length=3) 'cost_name' => string 'Carina' (length=6) 'cost_type' => string 'Customs' (length=7) 'cost_measure' => string 'Percent' (length=7) 'cost_amount' => string '5.00' (length=4) 'cost_id' => int 7 'group1' => string '' (length=3) 'group2' => string '' (length=3) 'group3' => string '' (length=3) 1 => array (size=9) 'cost_no' => string 'D02' (length=3) 'cost_name' => string 'Banka' (length=5) 'cost_type' => string 'Bank' (length=4) 'cost_measure' => string 'Percent' (length=7) 'cost_amount' => string '0.15' (length=4) 'cost_id' => int 8 'group1' => string '' (length=3) 'group2' => string '' (length=3) 'group3' => string '' (length=3) 2 => array (size=9) 'cost_no' => string 'D03' (length=3) 'cost_name' => string 'Špediter' (length=9) 'cost_type' => string 'Transport' (length=9) 'cost_measure' => string 'Percent' (length=7) 'cost_amount' => string '0.50' (length=4) 'cost_id' => int 9 'group1' => string '' (length=3) 'group2' => string '' (length=3) 'group3' => string '' (length=3) 3 => array (size=9) 'cost_no' => string 'D04' (length=3) 'cost_name' => string 'Troškovi firme' (length=15) 'cost_type' => string 'Tax' (length=3) 'cost_measure' => string 'Percent' (length=7) 'cost_amount' => string '21.00' (length=5) 'cost_id' => int 10 'group1' => string '' (length=3) 'group2' => string '' (length=3) 'group3' => string '' (length=3) 5 => array (size=9) 'cost_no' => string 'C1' (length=2) 'cost_name' => string 'Cost only for this group' (length=24) 'cost_type' => string 'Warehouse' (length=9) 'cost_measure' => string 'Percent' (length=7) 'cost_amount' => string '12.00' (length=5) 'cost_id' => int 12 'group1' => string 'ACC_P' (length=5) 'group2' => string '1401' (length=4) 'group3' => string '' (length=3) This is foreach and condition I tried to use to unset if non of the categories match ( it has to check from category3 to category 1). foreach ($cost_array as $key1 => $value1) { if (isset($form_group3) AND isset($value1['group3']) AND $value1['group3'] != $form_group3) { if (isset($form_group2) AND isset($value1['group2']) AND $value1['group2'] != $form_group2) { if (isset($form_group1) AND isset($value1['group1']) AND $value1['group1'] != $form_group1) { } else { unset($cost_array[$key1]); } } else { unset($cost_array[$key1]); } } else { unset($cost_array[$key1]); } } Groups user posts from form: $form_group1 = mysqli_real_escape_string($conn_mysqli, $_POST['group1']); $form_group2 = mysqli_real_escape_string($conn_mysqli, $_POST['group2']); $form_group3 = mysqli_real_escape_string($conn_mysqli, $_POST['group3']); The result I need to achieve is that all records where empty group3,group2,group1 have to stay and the one which has records in group3, group2 or group1 has to stay only if they match what user asks. So if form_group3 is same is array_group3, form_group2 is same as array_group2, form_group1 is same as array_group1 that record stays otherwise only that one is unset.
