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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.