Jump to content

CLI- reorder array keys after using 'unset'


MelWhetaer

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/67447-cli-reorder-array-keys-after-using-unset/
Share on other sites

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' ),
);

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

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);
?>

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?

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.