Replace key and value in an associative array and keep elements order

时间:2017-12-18 08:14:53

标签: php arrays

I have an array like:

df1 = pd.read_csv(get_work_folder_path(args.processName) + "/" + args.processName +"EnAlquiler"+ ".csv" , error_bad_lines=False)
df2 = pd.read_csv(get_work_folder_path(args.processName) + "/" + args.processName + ".csv" , error_bad_lines=False)

frames = [df1, df2]
result = pd.concat(frames)

df5 = pd.DataFrame(result)
df5.drop_duplicates( keep='first', inplace = True)

df5.to_csv(get_work_folder_path(args.processName) + "/" + args.processName +"HomeAwayComparacion"+ ".csv")

print(df5)

and I want to replace key and value $columns = [ 'id'=>'Id', 'status'=>'Status', 'created_at'=>'Created At' ]; with 'status'=>'Status'; I know that I can delete element by key using 'payment_status'=>'Payment status' and add the new element to the end or to the beginning of the array. But I want to keep elements order so the new element should be added after `id'.

3 个答案:

答案 0 :(得分:2)

One way I think of is using let queue = DispatchQueue(label: "label") queue.async { queue.sync { // outer block is waiting for this inner block to complete, // inner block won't start before outer block finishes // => deadlock } // this will never be reached } to get key as offset. Then using array_search() with array_merge()

array_slice()

答案 1 :(得分:0)

One of the way is, to create another array and assign it to your exiting array, this way your original array will be replaced.

insert into table select toNanoTime() from other_table;

答案 2 :(得分:0)

您可以使用array_map仅遍历整个数组一次,并创建一个新的新列名称数组。

$myArray=array("id"=>123,"status"=>"no paid");
$otherArray = array();
$columns = array(
        'id'=>'Id',
        'status'=>'Status',
        'created_at'=>'Created At'
);


array_map(function($column,$key) {
    if(array_key_exists($key,$GLOBALS['columns']))
        $GLOBALS['otherArray'][$GLOBALS['columns'][$key]]=$column;
    else
        $GLOBALS['otherArray'][$key]=$column;
    return $column;

}, $myArray,array_keys($myArray));
print_r($otherArray);

以下是demo