PHP array bug

I have 2 arrays one is called checked and one is called changed. I need to transform one element from each array and I want to add a condition that only if $transformLValue and $transformRValue equal. In the array element [0] is the left value and element [3] is the right, I apply a transformation on the left value to basically get the value after a delimiter (using !!) and on the right I use a string as the delimiter called “/stories” (it’s just how the data is formatted). So the left and right match in correctly but the results using the condition is unexpected sometimes value don’t match but still show like they do. What am I doing wrong? Thanks for any help

for ($i = 0; $i < $count; $i++) {


  if (!empty($changed[$i]) && isset($changed[$i]) && !is_null($changed[$i]))
  {

   for ($jj =0; $jj < $count2; $jj++)
    {
      


      $resultVal = $changed[$i] . "&&&" . $checked[$jj];
      $unbinditem = array_pad(explode("&&&",$resultVal), 5, null);
     
    

      $transformLValue = explode("!!",$unbinditem[0]); 
      $transformRValue = explode("/stories",$unbinditem[3]); 
      


      if($transformLValue[$jj] == $transformRValue[$jj]){
        echo $transformLValue[1] . " vs " . $transformRValue[1];

blinks slowly

I started to type out a long post trying to walk through the idea, but… okay.

Can you give us an example of what is in $changed[0] and $checked[0]? It may be easier to walk through with a concrete example.

Ok yes that will be helpful, below I am pasting the array in the loop where these are combined and the overall goal

Array ( [0] => #rpg!!27C.html 
	[1] => 01/08/2023 14:23 
	[2] => some text1 
	[3] =>  /stories27C.html 
	[4] =>  #crowd ) 

Array ( [0] => #rpg!!27C.html 
	[1] => 01/08/2023 12:59 
	[2] => some text2 
	[3] =>  /stories27F.html 
	[4] =>  #crowd ) 

Array ( [0] => #rpg!!27C.html 
	[1] => 01/08/2023 12:03 
	[2] => some text3 
	[3] =>  /stories27H.html 
	[4] =>  #crowd ) 

Array ( [0] => #foot!!27F.html 
	[1] => 01/08/2023 14:23 
	[2] => some text1 
	[3] =>  /stories27C.html 
	[4] =>  #crowd ) 

Array ( [0] => #foot!!27F.html 
	[1] => 01/08/2023 12:59 
	[2] => some text2 
	[3] =>  /stories27F.html 
	[4] =>  #crowd ) 

Array ( [0] => #foot!!27F.html 
	[1] => 01/08/2023 12:03 
	[2] => some text3 
	[3] =>  /stories27H.html 
	[4] =>  #crowd ) 

Array ( [0] => #cnt!!27H.html 
	[1] => 01/08/2023 14:23 
	[2] => some text1 
	[3] =>  /stories27C.html 
	[4] =>  #crowd ) 

Array ( [0] => #cnt!!27H.html 
	[1] => 01/08/2023 12:59 
	[2] => some text2 
	[3] =>  /stories27F.html 
	[4] =>  #crowd ) 

Array ( [0] => #cnt!!27H.html 
	[1] => 01/08/2023 12:03 
	[2] => some text3 
	[3] =>  /stories27H.html 
	[4] =>  #crowd ) 

so the goal is when these elements match the condition is met but my output in the condition has

27C.html vs 27H.html (added: why if not equal?)
27F.html vs 27F.html
27F.html vs 27H.html (added: why if not equal?)
27H.html vs 27H.html
(added: missing 1)

I am thinking…everything.

What is the real problem you are trying to solve with this code? What is the high level overview of what you have going on?

Where is this array coming from? A database?

Checked and changed come from post data on an earlier form.

$checked = $_POST[‘check’];
$changed = $_POST[‘hashtag’];

Initially I just had checked but wanted to add changed because that is a text box I manually enter a value in. So I had to add that in to a script that was working, I thought at first merge the arrays and I didn’t have luck with that. Then if all is good I take each element and add to an insert statement so each row inserts correctly.

Any Ideas?

You could always try answering the questions that have been asked of you.

1 Like

The big question of WHAT you wish to accomplish, has been asked as it is important to clarify and communicate with others the specifc plan in order to reach the intended goal.
Looking at a single array set you have these values.

array ( 
	 0 => '#rpg!!27C.html'
	,1 => '01/08/2023 14:23'
	,2 => 'some text1'
	,3 => '/stories27C.html'
	,4 => '#crowd' 
)

Here:

$resultVal = $changed[$i] . "&&&" . $checked[$jj];

Your attempt to combine a $changed[$i] array with a $checked[$jj] gives you an "Notice: Array to string conversion ERROR" each time the for ($i and for ($jj loops though. If you entered this 9 record array into each input your code would be choking 81 times on this line alone not to mention the lines dealing with $resultVal.

The big question is WHY combine them to then turn around and explode them into separate arrays again? Does that make sense?

So WHAT do you want to do? As I understand it you wish to compare the end of KEY 0 of $changed[$i] to the end of KEY 3 of $checked[$jj].

SO why not explode those values directly?
$changed[$i][0] holds the value of your outside loop right?

$transformLValue = explode("!!",$changed[$i][0]);

Now technically THIS “exploded array” should only be defined ONCE in the for ($i loop so it should be moved UP so it is not in the for ($jj loop being redefined multiple times.
Then in your for ($jj loop you will explode the $checked[$jj][3] array on “/stories”.

$transformRValue = explode("/stories",$checked[$jj][3]);

Remember these transformValue variables are arrays with natural KEYS 0 and 1 and as you wish to compare the end of string, which is now 2 parts so you would use KEY 1, SO your IF condition would be written as

if($transformLValue[1] == $transformRValue[1]){

So NOW after all this we are back to that BIG question of what do you wish to accomplish?
Remember your code is looping through all the values of $changed then for each of those sub arrays it is looping through $checked looking for a match. I believe I got 27 matches when running your array on itself.
I went ahead and echoed $jj to double check the values of /stories to the $changed value. My version

$count = count($changed);
$count2 = count($checked);

for ($i = 0; $i < $count; $i++) {

	if (!empty($changed[$i]) && isset($changed[$i]) && !is_null($changed[$i]))
	{
		
		//Returns 2 part array
		$transformLValue = explode("!!",$changed[$i][0]);
		
		for ($jj =0; $jj < $count2; $jj++)
		{ 		  
			//Returns 2 part array
			$transformRValue = explode("/stories",$checked[$jj][3]); 
			
			if($transformLValue[1] == $transformRValue[1]){
				echo $jj . " : " . $transformLValue[1] . " vs " . $transformRValue[1]."<br />";
			}
		}
	}
}

showing all the keys of matches to the 9 original records

0 : 27C.html vs 27C.html
3 : 27C.html vs 27C.html
6 : 27C.html vs 27C.html
0 : 27C.html vs 27C.html
3 : 27C.html vs 27C.html
6 : 27C.html vs 27C.html
0 : 27C.html vs 27C.html
3 : 27C.html vs 27C.html
6 : 27C.html vs 27C.html
1 : 27F.html vs 27F.html
4 : 27F.html vs 27F.html
7 : 27F.html vs 27F.html
1 : 27F.html vs 27F.html
4 : 27F.html vs 27F.html
7 : 27F.html vs 27F.html
1 : 27F.html vs 27F.html
4 : 27F.html vs 27F.html
7 : 27F.html vs 27F.html
2 : 27H.html vs 27H.html
5 : 27H.html vs 27H.html
8 : 27H.html vs 27H.html
2 : 27H.html vs 27H.html
5 : 27H.html vs 27H.html
8 : 27H.html vs 27H.html
2 : 27H.html vs 27H.html
5 : 27H.html vs 27H.html
8 : 27H.html vs 27H.html

Based on the information you’ve provided, it’s difficult to say exactly what is causing the unexpected behavior in your code. However, there are a few things that you might want to check:

  1. Are you comparing the correct elements from the arrays? Based on your description, it sounds like you’re comparing element [0] from the checked array to element [3] from the changed array. Make sure that these are the correct elements you want to compare.
  2. Are you using the correct delimiter for both arrays? You mentioned that you’re using “!!” for the left value and “/stories” for the right value. Make sure that you’re using the correct delimiter for each array.
  3. Are you handling any special characters or whitespace correctly? If the left and right values contain any special characters or whitespace, make sure that they are being handled correctly in your comparison.
  4. Are you comparing the result of the transformation as expected? Make sure that the result of the transformation is of the same type that you are expecting, if it’s a string or number.
  5. Are you updating the array element after the comparison? If the comparison is successful, make sure that you’re updating the corresponding element of the array.

It’s also worth looking at the data, you might want to print the output of the transformation on the left and right values, and compare them to see if they match as expected. Also, check the documentation of the method you’re using for the transformation and make sure you’re using it correctly.

If you keep having trouble, it would be helpful to see some sample code and sample data, so I can take a closer look and provide more specific advice.

A post was split to a new topic: Problem finding number in array

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.