madkris Posted June 21, 2009 Share Posted June 21, 2009 hi, im new to php and was trying to do a sort on an array without using the predefined ones, everything else seems to be working except for this part.. was hoping you guys could help me out. thanks in advance.. <?php $array1 = array(4,8,2,7,1); $smallest; //find smallest value in array function findSmallest($array){ $size = count($array); $GLOBALS['smallest'] = $array[0]; for($i = 0; $i < $size; $i++) { if($GLOBALS['smallest'] > $array[$i+1]){ $GLOBALS['smallest'] = $array[$i+1]; } //echo $GLOBALS['smallest'] . "<br>"; } return $GLOBALS['smallest']; } findSmall($array1); echo '<pre>'; var_dump($GLOBALS['smallest']);//returns null echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/ Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 ewww... $GLOBALS. I'll rewrite that without $GLOBALS. <?php $array1 = array(4,8,2,7,1); //find smallest value in array function findSmallest($array) { return empty($array)? false : min($array); } $min = findSmallest($array1); echo '<pre>'; var_dump($min); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860485 Share on other sites More sharing options...
taquitosensei Posted June 21, 2009 Share Posted June 21, 2009 You don't need to use $GLOBALS here. Just use a variable in your function then assign the result to a variable. And you function is findSmallest not findSmall. $array1 = array(4,8,2,7,1); $smallest; //find smallest value in array function findSmallest($array){ $size = count($array); $smallest = $array[0]; for($i = 0; $i < $size; $i++) { if($smallest > $array[$i+1]){ $smallest = $array[$i+1]; } //echo $GLOBALS['smallest'] . "<br>"; } return $smallest; } $smallest=findSmallest($array1); // or if you need to use $GLOBALS $GLOBALS['smallest']=findSmallest($array1); echo '<pre>'; var_dump($smalles) // or var_dump($GLOBALS['smallest']); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860486 Share on other sites More sharing options...
madkris Posted June 21, 2009 Author Share Posted June 21, 2009 ewww... $GLOBALS. I'll rewrite that without $GLOBALS. sorry only way i could do it as of now, im still kinda confused with using passed by reference '&$variable'... your code looks great might give me a minute to trace thou.. And you function is findSmallest not findSmall. sorry bout that, originally it was findSmall, changed it to findSmallest for readability and yes i have tried your approach too unfortunately when i do an echo or print_r on variable $smallest. it doesnt show anything. i made some comments on my code below. kindly read them to understand what ive figured out so far. thanks $array1 = array(4,8,2,7,1); function findSmallest($array){ $size = count($array); $smallest = $array[0]; for($i = 0; $i < $size; $i++) { if($smallest > $array[$i+1]){ $smallest = $array[$i+1]; } //if i echo $smallest here it returns 4,2,2,1 respectively which means it should return value 1 } //if i do an echo here it doesnt show anything return $smallest; } function mysort($array) { $smallest = findSmallest($array); print_r($smallest); // doesnt show anything } mysort($array1); Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860497 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 There's a problem with your method. What if $array is an empty array? Try my function. Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860501 Share on other sites More sharing options...
madkris Posted June 21, 2009 Author Share Posted June 21, 2009 There's a problem with your method. What if $array is an empty array? Try my function. eureka! now im stuck with a new problem my while loop stops at 1st pass, any idea what im doing wrong? <?php $array1 = array(4,8,2,7,1); $array2 = array(); //store smallest value into $array2 function getSmall($array, $key) { $temp = array_splice($array,$key,1); foreach($temp as $value){ $GLOBALS['array2'][] = $value; } $GLOBALS['array1'] = array_diff($GLOBALS['array1'],$GLOBALS['array2']); } //find the smallest value in $array1 function findSmallest($array) { return empty($array)? false : min($array); } //get index of smallest value function getSmallIndex($array, $small) { $key = array_search($small, $array); return $key; } //sort function mysort($array) { while(count($array) > 0) { $smallest = findSmallest($array); $key = getSmallIndex($array,$smallest); $array = getSmall($array,$key); } } mysort($array1); echo '<pre>array1:'; var_dump($GLOBALS['array1']); echo '</pre><br>'; echo '<pre>array2:'; var_dump($GLOBALS['array2']); echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860520 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 What are you trying to do? Sort the array? If so, there is the sort function. <?php $unsortedarray = $sortedarray = array(4,8,2,7,1); sort($sortedarray); var_dump($unsortedarray, $sortedarray); Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860523 Share on other sites More sharing options...
madkris Posted June 21, 2009 Author Share Posted June 21, 2009 What are you trying to do? Sort the array? If so, there is the sort function. Yes, I am trying to sort the array without using the predefine sort functions in php. Well now my while loop goes on an infinite loop. Any idea what I'm doing wrong? $array1 = array(4,8,2,7,1); $array2 = array(); //store smallest value into $array2 function getSmall($array, $key) { $temp2 = array_splice($array,$key,1); foreach($temp2 as $value){ $GLOBALS['array2'][] = $value; } $temp1 = array_diff($GLOBALS['array1'],$GLOBALS['array2']); $GLOBALS['array1'] = $temp1; return $temp1; } //find the smallest value in $array1 function findSmallest($array) { return empty($array)? false : min($array); } //get index of smallest value function getSmallIndex($array, $small) { $key = array_search($small, $array); return $key; } //sort function mysort($array) { while(count($array) > 0) { $smallest = findSmallest($array); $key = getSmallIndex($array,$smallest); $array = getSmall($array,$key); echo '<pre>array1:'; var_dump($GLOBALS['array1']); echo '</pre>'; echo "<br>"; echo '<pre>array2:'; var_dump($GLOBALS['array2']); echo '</pre>'; } } mysort($array1); here is the output before my browser crashes due to the infinite loop 5// array length before getSmall function inside my mysort function 4//array length after getSmall function inside my mysort function array1:array(4) { [0]=> int(4) [1]=> int( [2]=> int(2) [3]=> int(7) } array2:array(1) { [0]=> int(1) } 4 3 array1:array(3) { [0]=> int(4) [1]=> int( [3]=> int(7) } array2:array(2) { [0]=> int(1) [1]=> int(2) } 3 2 array1:array(2) { [1]=> int( [3]=> int(7) } array2:array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(4) } 2 2 array1:array(2) { [1]=> int( [3]=> int(7) } array2:array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(4) } 2 2 array1:array(2) { [1]=> int( [3]=> int(7) } array2:array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(4) } Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860524 Share on other sites More sharing options...
madkris Posted June 21, 2009 Author Share Posted June 21, 2009 I was tracing my code and i found the problem, still trying to figure out why this part gets the value of zero thou. Ideas? $array1 = array(1=>8, 3=>7); $array2 = array(0=>1,1=>2, 2=>4); //same code as the one posted above.. $smallest = findSmallest($array1); $key = getSmallIndex($array1,$smallest); echo "key :<b>" . $key . "</b> smallest :<b>" . $smallest ."</b><br><br>"; $temp2 = array_splice($array1,$key,1); //evaluates to array(0) {} not sure why : array_splice($array1,$key,1) ==> array_splice($array1,3,1) ==> array_splice(array,offset,length) echo '<pre>$array:'; var_dump($temp2); echo '</pre><br>'; foreach($temp2 as $value){ echo "$value :" . $value; $GLOBALS['array2'][] = $value; } output: key :3 smallest :7 $array:array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860533 Share on other sites More sharing options...
madkris Posted June 21, 2009 Author Share Posted June 21, 2009 SOLVE! thanks for the help guys.. Quote Link to comment https://forums.phpfreaks.com/topic/163085-unable-to-return-global-variable/#findComment-860557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.