Jump to content
budimir

How to set condition to remove from array?

Recommended Posts

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.

 

Share this post


Link to post
Share on other sites

Does the data in that array come from a database? If so, why not put those conditions in your query instead of pulling all the records then removing some?

Share this post


Link to post
Share on other sites

'group3' => string '' (length=3)

 

Why does an empty string have a length of 3?

 

If you're going to compare two strings, it doesn't make sense to run one of them through mysqli_real_escape_string() first, does it? Now the string is altered, potentially. You should only use that function (if it's even supposed to be used at all) when you're preparing data for a database query.

Share this post


Link to post
Share on other sites

Something like this?

foreach ($cost_array as $key1 => $value1) {

	// group 1, group 2 and group 3 are empty and should stay => continue the foreach loop
	if (empty($value1['group1']) && empty($value1['group2']) && empty($value1['group3']))
		continue;
	
	// if form group 1 exists and its value is the same as $value group 1, it should stay => continue the foreach loop
	if (isset($form_group1) && $value1['group1'] == $form_group1)
		continue;
	
	// if form group 2 exists and its value is the same as $value group 2, it should stay => continue the foreach loop
	if (isset($form_group2) && $value1['group2'] == $form_group2)
		continue;
	
	// if form group 3 exists and its value is the same as $value group 3, it should stay => continue the foreach loop
	if (isset($form_group3) && $value1['group3'] == $form_group3)
		continue;
	
	// otherwise unset the value
	unset($cost_array[$key1]);
	
}
  • Like 1

Share this post


Link to post
Share on other sites

I think if you reversed that to start with group3 and go down to group1, that would match the OP's intent.

 

Does unsetting values of an array you're currently looping through with foreach() create any side effects?

Share this post


Link to post
Share on other sites

Thanks everyone for the help! 

 

@cloetensbrecht, Your answer pushed me to right direction. I had to change the conditions a bit, but it was your answer that gave me the idea. Thank you so much! I spent four days trying to figure it out!

 

This is code that gives proper result:

    foreach ($cost_array as $key1 => $value1) {
   // group 1, group 2 and group 3 are empty and should stay => continue the foreach loop
    if (empty($value1['group1']) && empty($value1['group2']) && empty($value1['group3']))
        continue;

    //When all three exist and are same it should continue
    if (!empty($form_group3) && $value1['group3'] == $form_group3 && !empty($form_group2) && $value1['group2'] == $form_group2 && !empty($form_group1) && $value1['group1'] == $form_group1)
        continue;

    if ($value1['group3'] != $form_group3 && !empty($form_group2) && $value1['group2'] == $form_group2 && !empty($form_group1) && $value1['group1'] == $form_group1)
        continue;
        
    if (!empty($form_group2) && $value1['group2'] == $form_group2 && !empty($form_group1) && $value1['group1'] == $form_group1)
       continue;

    if (!empty($form_group3) && !empty($form_group2) && !empty($form_group1) && $value1['group1'] == $form_group1)
      continue;
    
    //This covers situation where we have category and subcategory that match
    if (!empty($form_group2) && $value1['group2'] == $form_group2 && !empty($form_group1) && $value1['group1'] == $form_group1)
      continue;
    
    //This covers all situations where we have success only on category but nothing else  
    if (!empty($form_group1) && $value1['group1'] == $form_group1 && empty($value1['group2']) && empty($value1['group3']))
      continue;
	
    // otherwise unset the value
    unset($cost_array[$key1]);
	
}

 I still need to catch one of the conditions, but that's what does the job. I'm sure it can be written in more elegant way. :) If there is any suggestions how to do it more elegant I would appreciate it.

Edited by budimir

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.