EchoFool Posted November 3, 2010 Share Posted November 3, 2010 Hey I have an array comparison which returns values in an array but then i need to change values that are 1 in the array to 14 so currently i use a loop but i get undefined offset issues. How can i change it to work properly with error? This is my current attempt: $HC = array_diff($Values,$Temp); $i = 0; foreach($HC as $value) { if($HC[$i] == 1) { $HC[$i] = 14; } $i++; } Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/ Share on other sites More sharing options...
OldWest Posted November 3, 2010 Share Posted November 3, 2010 I'm not sure if your loops work correctly, but the first thing I would try is adding some ' ' around your 1 like '1' ... not totally sure, but that might be parsing as TRUE instead of value of 1. Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129786 Share on other sites More sharing options...
OldWest Posted November 3, 2010 Share Posted November 3, 2010 And I am pretty sure array_diff() is not the correct function to use on your array. Assuming you have 2 arrays and you want to find the differences in them, arg1, arg2 in that function.. array_diff would output anything that is in arg1 that does not exist in arg2 - arg 1 and 2 being arrays of course... Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129788 Share on other sites More sharing options...
anups Posted November 3, 2010 Share Posted November 3, 2010 if array undefined offset is the issue you can use isset $HC = array_diff($Values,$Temp); $i = 0; foreach($HC as $value) { if(isset($HC[$i]) && $HC[$i] == 1) { $HC[$i] = 14; } $i++; } Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129799 Share on other sites More sharing options...
EchoFool Posted November 3, 2010 Author Share Posted November 3, 2010 if array undefined offset is the issue you can use isset $HC = array_diff($Values,$Temp); $i = 0; foreach($HC as $value) { if(isset($HC[$i]) && $HC[$i] == 1) { $HC[$i] = 14; } $i++; } I thought of using this but thought - surely the for loop wouldn't loop if it was not set anyway because it only loops whilst it is set no ? Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129803 Share on other sites More sharing options...
Buddski Posted November 3, 2010 Share Posted November 3, 2010 You are getting undefined offsets because they dont exist in the array.. use the key of the array like so.. $HC = array_diff($Values,$Temp); foreach($HC as $key=>$value) { if($value == 1) { $HC[$key] = 14; } } Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129820 Share on other sites More sharing options...
AbraCadaver Posted November 3, 2010 Share Posted November 3, 2010 You are getting undefined offsets because they dont exist in the array.. use the key of the array like so.. $HC = array_diff($Values,$Temp); foreach($HC as $key=>$value) { if($value == 1) { $HC[$key] = 14; } } Yes, or you can change it directly using a reference: foreach($HC as &$value) { if($value == 1) $value = 14; } Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129947 Share on other sites More sharing options...
EchoFool Posted November 3, 2010 Author Share Posted November 3, 2010 Thanks guys for the help! Link to comment https://forums.phpfreaks.com/topic/217621-help-with-array-issue/#findComment-1129988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.