0

I am trying to access certain subarrays of an array depending on a sha1 code saved in the subarray. I have the following code working for me:

//I take a sha1 string of 40chars from GET
if(isset($_REQUEST['sha1'])){

  //then I iterate through the array and check if the subarrays sha1 matches
  for($i = 0; $i < count($scomplaints); $i++) {
    if($scomplaints[$i]['sha1'] == $_REQUEST['sha1']){

      //once a match is found I delete the match...
      unset($scomplaints[$i]);

      //... reserialize the array and update it in the database
      $new_c = serialize($scomplaints);
      // ( add new array to database )
      break;
    }
  }
}

Pretty straight forward. Only problem is: In some random cases the loop doesnt read the sha1 value of the subarray. Instead it reads an empty string from the array.

But when i var_dump the array the sha1 code is clearly saved in the array and also identical to the one I get from GET (I did a by hand comparison).

I can't figure out what the problem is, maybe some encoding error? Maybe one of you guys can help me with this?

Thanks for all advice.

7
  • Your description of what's going wrong is very vague. What do you mean with 'Instead it gets an empty response'. What happens if you var_dump both arrays when it breaks? Commented Aug 21, 2012 at 15:28
  • 1
    Please provide cases where it breaks (array output and GET value). Commented Aug 21, 2012 at 15:28
  • Yeah was a bit fuzzy I updatet it but your response was way to fast for me :D Hope its clearer now. Commented Aug 21, 2012 at 15:29
  • It breaks here: array: "ab5cec9f67b5b877725a1eb4baae304f9b54cd62", sha1: "ab5cec9f67b5b877725a1eb4baae304f9b54cd62". The array value is what var_dump tells me is in the array, but the loop just returns an empty string (string length 0) Commented Aug 21, 2012 at 15:30
  • Please could you update your code above with the debug messages? Also some sample data would be helpful. Commented Aug 21, 2012 at 15:40

1 Answer 1

0

I got it solved. The issue was the array not being reindexed by unset($array). Once I reindex it with array_values($array) it works again.

Your Answer

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.