0

So this is how my Array ($dataArray) looks oks like:

Array 
( 
 [0] => Array 
 ( 
 [0] => Date 
 [1] => Time 
 [2] => Duration 
 [3] => Info 
 [4] => Client
 )
 [1] => Array 
 ( 
 [0] => 2021年12月01日 
 [1] => 10:45:43 
 [2] => 237 
 [3] => Some text from 
 [4] => Client 1 
 ) 
 [2] => Array 
 ( 
 [0] => 2021年12月01日 
 [1] => 11:29:13 
 [2] => 77 
 [3] => Nothing important 
 [4] => Client 2 
 ) 
 [3] => Array 
 ( 
 [0] => 2021年12月01日 
 [1] => 11:53:03 
 [2] => 44 
 [3] => anonymous 
 [4] => Client 1 
 )

I need to Loop trough it to search the Client Names, and if i find the matching name in the Element 4 then delete the entire Array.

$ExportKDname = "Client 1"
foreach($dataArray as $key => $sub_array) {
 if($sub_array[4] == $ExportKDname) {
 unset($dataArray[$key]);
 break; 
 }
}
 
 print_r($dataArray);

But with this code none of the arrays will be deleted. And I just can not find the right way to do it.

The Final array what I need to look like if we find the "Client 1" in the array would be like this:

Array 
( 
 [0] => Array 
 ( 
 [0] => Date 
 [1] => Time 
 [2] => Duration 
 [3] => Info 
 [4] => Client
 )
 [1] => Array 
 ( 
 [0] => 2021年12月01日 
 [1] => 11:29:13 
 [2] => 77 
 [3] => Nothing important 
 [4] => Client 2 
 ) 
asked Jan 13, 2022 at 13:11
6
  • That code should work just fine, in fact it does. It will only delete the first occurance it finds of course Commented Jan 13, 2022 at 13:20
  • You're missing a semicolon on your exportKDname variable. However my guess is that you have whitespace somewhere in your names as Riggs is correct that the code would generally work fine. Trim both your subfield and your exportDKname variable. Commented Jan 13, 2022 at 13:21
  • First of all, why you put break If you need to remove 2 elements? Commented Jan 13, 2022 at 13:21
  • Remove break, also be sure that $sub_array[4] is not containing any extra spaces, for that you can do if (trim($sub_array[4]) == $ExportKdName) Commented Jan 13, 2022 at 13:22
  • Demo of code working, your code or comment the break; to remove all occurances of Client 1 Commented Jan 13, 2022 at 13:24

2 Answers 2

1

In the if condition you are saying "if u match with $sub_arr[4] == $ExportKDname unset it and stop the loop". the machine doing that. when it matched first time it removes and stoping. If u wanna delete all match do not write break; let it continue. So delete or make it comment break; line.

answered Jan 13, 2022 at 13:37
Sign up to request clarification or add additional context in comments.

Comments

1

You can array_filter your variable and check if value is in_array.

With PHP 7.4+ syntax it should look like this:

$result = array_filter($dataArray, fn ($innerArray) => !in_array('Client 1', $innerArray));
answered Jan 13, 2022 at 13:19

1 Comment

Arrow functions were introduced in PHP 7.4

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.