MelWhetaer Posted August 31, 2007 Share Posted August 31, 2007 Hi all I'm having a problem finding a sort function that will reorder the keys in an array after I delete one of the array key/value pair. So if I have array: $list = array( '0'=>array( 'one', 'two', 'three' ), '1'=>array( '001', '1', 'c' ), '2'=>array( '001', '2', 'f' ), '3'=>array( '002', '3', 'i' ), ); and unset($list[1][0]); how do I reorder the array keys so that I get: $list = array( '0'=>array( 'one', 'two', 'three' ), '1'=>array( '001', '2', 'f' ), '2'=>array( '002', '3', 'i' ), ); instead of $list = array( '0'=>array( 'one', 'two', 'three' ), '2'=>array( '001', '2', 'f' ), '3'=>array( '002', '3', 'i' ), ); hope you can help Thank you Melanie Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/ Share on other sites More sharing options...
Daniel0 Posted August 31, 2007 Share Posted August 31, 2007 You might want to use array_shift() to remove the first element. Also, it should be $list = array( 0=>array( 'one', 'two', 'three' ), 1=>array( '001', '2', 'f' ), 2=>array( '002', '3', 'i' ), ); so you use integers instead of strings, but in that case you wouldn't need to specify the key name as it would be automatically given. $list = array( array( 'one', 'two', 'three' ), array( '001', '2', 'f' ), array( '002', '3', 'i' ), ); Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338614 Share on other sites More sharing options...
MelWhetaer Posted August 31, 2007 Author Share Posted August 31, 2007 Hi Daniel thanks for the feedback, i've removed the keys from my program only i'm still getting the same results. I get an error "undefined offset: 1" which refers to the key '1' not being present in the array. Do you know what the problem is here? Mel Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338621 Share on other sites More sharing options...
sasa Posted August 31, 2007 Share Posted August 31, 2007 try <?php function reorder_key($a){ if (is_array($a)){ $out = array(); foreach ($a as $v) $out[] = reorder_key($v); } else $out = $a; return $out; } $list = array( '0'=>array( 'one', 'two', 'three' ), '1'=>array( '001', '1', 'c' ), '2'=>array( '001', '2', 'f' ), '3'=>array( '002', '3', 'i' ), ); unset($list[1][0]); $list = reorder_key($list); print_r($list); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338634 Share on other sites More sharing options...
Daniel0 Posted August 31, 2007 Share Posted August 31, 2007 Hi Daniel thanks for the feedback, i've removed the keys from my program only i'm still getting the same results. I get an error "undefined offset: 1" which refers to the key '1' not being present in the array. Do you know what the problem is here? Mel Could you show the code that produces that error? Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338636 Share on other sites More sharing options...
MelWhetaer Posted August 31, 2007 Author Share Posted August 31, 2007 here is the code that produces the error: $list = array( array( 'one', 'two', 'three' ), array( '001', '1', 'c' ), array( '001', '2', 'f' ), array( '002', '3', 'i' ), ); $del = 0; for($i=0; $i<count($list)-$del; $i++) { $comp = $list[$i][0]; $comp2 = $list[$i][1]; $listRem = count($list)-$i; for($j=$i+1; $j<$listRem; $j++) { if(isset($list[$j])) echo "comp-->" . $comp . "-" . $comp2 . " \n"; echo "\tList-->" . $list[$j][0] . "-" . $list[$j][1] . " \n"; if( $comp == $list[$j][0] ) { echo "\n++++++ " . $list[$j][1] . " was deleted\n\n"; unset($list[$j]); $del = $del++; } } /*ksort($list);*/ } Mel Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338644 Share on other sites More sharing options...
MelWhetaer Posted August 31, 2007 Author Share Posted August 31, 2007 try <?php function reorder_key($a){ if (is_array($a)){ $out = array(); foreach ($a as $v) $out[] = reorder_key($v); } else $out = $a; return $out; } $list = array( '0'=>array( 'one', 'two', 'three' ), '1'=>array( '001', '1', 'c' ), '2'=>array( '001', '2', 'f' ), '3'=>array( '002', '3', 'i' ), ); unset($list[1][0]); $list = reorder_key($list); print_r($list); ?> Thanks sasa that works great, i've been trying to work out how the if statement works in the 'reorder_key funcation. Am I right in thinking that if there is a key missing in the key list then the 'is_array' function returns False? Thanks again Mel Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338657 Share on other sites More sharing options...
sasa Posted August 31, 2007 Share Posted August 31, 2007 no, it just chek is it input an array it is recursive function it is build that first solve the simple problem (if input is NOT array just return it (else part)) recursive step: if is array build new array ($out) with values of input array and before add vallue ($v) in $out array rebuild key for it Quote Link to comment https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/#findComment-338681 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.