PHP:从数组中删除空值并向上移动值

I'm trying to create a function that shifts array values up a key if the previous key is empty and one after is set. E.g. this array:

array (size=4)
    'row1' => string 'row1' (length=4)
    'row2' => string '' (length=0)
    'row3' => string '' (length=0)
    'row4' => string 'row4' (length=4)

should become this after my function call:

array (size=4)
    'row1' => string 'row1' (length=4)
    'row2' => string 'row4' (length=4)
    'row3' => string '' (length=0)
    'row4' => string '' (length=0)

I do have a working function, however, it uses a lot of if statements and I'm 100% sure that it could be done more efficiently, any ideas on how to achieve efficiently?

Thanks

Lets try this with a big array, and hope this will help you out. The way of question is different but your question is same as

1. Getting empty values at end.

2. Non empty at starting without changing order

3. Without changing keys order

If you think about any array this result come to end.

Try this code snippet here

<?php

$array=$tempArray=array(
    'row1' =>  'row1',
    'row2' =>  '' ,
    'row3' =>  '',
    'row6' =>  'row6' ,
    'row4' =>  'row4' ,
    'row5' =>  '',
    'row7' =>  'row7' ,
    'row8' =>  ''
);
$result=array();
$tempArray=  array_values($tempArray);
$tempArray=array_values(array_filter($tempArray));

foreach(array_keys($array) as $key_key => $key)
{
    if(!empty($tempArray[$key_key]))
    {
        $result[$key]=$tempArray[$key_key];   
    }
    else
    {
        $result[$key]="";
    }
}
print_r($result);

Output:

Array
(
    [row1] => row1
    [row2] => row6
    [row3] => row4
    [row6] => row7
    [row4] => 
    [row5] => 
    [row7] => 
    [row8] => 
)
  • unfortunately you did not share your code, so we cannot know what you do exactly
  • also it would be helpful if you added the array as code we can run directly and do not need to edit/re-create

I would

  • ignore the keys for processing the list when possible
  • unset entries that are nor needed any longer
  • iterate lists with foreach

apart from this you can sort lists in PHP (by value, key; asc, desc; with/without association), they have a nice overview on that in the manual.

example steps:

<?php

$a['row1'] = 'row1';
$a['row2'] = '';
$a['row3'] = '';
$a['row4'] = 'row4';

print_r( $a );

arsort($a);

print_r( $a );

//get rid of double entries
$f=array_flip( $a );
//get rid of empty entries
unset($f['']);

print_r( array_flip( $f ) );

?>

EDIT

 $yourarr=array('row1'=>"row1",'row2'=>"",'row3'=>"",'row4'=>"row4");
    $array1=array();
    $array2=array();
    foreach ($yourarr as $key =>$val){
    if(empty($val)){
    $array2[$key]=$val;
    }else{
    $array1[$key]=$val;
    }
    }
    $newarr=array_merge($array1,$array2);
    //<!-- Try This if you want to remove the empty indexes just put ! infront of the empty and delete the else part and to reset the row count -->

    $i=1;
    $newarr2=array();
    foreach($newarr as $key =>$val){
    $newarr2['row'.$i]=$val;
    $i++;
    }
    var_dump($newarr2);

OUTUT

     D:\wamp64\www\test\index.php:21:
array (size=4)
  'row1' => string 'row1' (length=4)
  'row2' => string 'row4' (length=4)
  'row3' => string '' (length=0)
  'row4' => string '' (length=0)

You can do this in a single line, making use of array_ functions.

$o = array_combine(array_keys($input), array_pad(array_filter($input), count($input), ''));
  • array_filter will, by default, remove any empty values from the array
  • array_pad will pad the array to the length of the original array, adding an empty string
  • array_keys will get the keys of the original array
  • array_combine will combine the keys with the values

The above would output the following:

array (size=4)
  'row1' => string 'row1' (length=4)
  'row2' => string 'row4' (length=4)
  'row3' => string '' (length=0)
  'row4' => string '' (length=0)

Here's a demo